如何通过字段类型获取字段的值

问题描述 投票:0回答:1

我有一个网络表单处理程序,我需要获取字段类型

#webform_document_file
的所有文件 ID。其中有多个字段名称不同但类型相同,希望优化查询。

类似的东西

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

drupal drupal-webform
1个回答
0
投票

你能尝试一下这样的事情吗

// 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'.

© www.soinside.com 2019 - 2024. All rights reserved.