How to get the value of a field by the field type

53 Views Asked by At

I have a webform handler and I need to get all the file Ids of field type #webform_document_file. There are multiple of these fields with different field names but same type, would like to optimize the query.

something like

$webform_submission->getFieldDefinitionType('#webform_document_file')->getValue();

1

There are 1 best solutions below

0
Selvakumar Eswaran On

Can you please try something like this,

// Get the webform submission.
$webform_submission = $this->getEntity();

// Initialize an array to store file IDs.
$file_ids = [];

// Iterate through the webform submission fields.
foreach ($webform_submission->getElementsDecodedAndFlattened() as $key => $value) {
    // Check if the field type is '#webform_document_file'.
    if (!empty($value['#type']) && $value['#type'] === 'webform_document_file') {
        // Check if the field has a value.
        if (!empty($value['#value'])) {
            // If the field has a value, add its ID to the array.
            $file_ids[] = $value['#value'];
        }
    }
}

// Now $file_ids array contains all the file IDs of fields with type '#webform_document_file'.