You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按字段类型获取字段值?Webform多文件字段ID提取

批量获取Webform提交中指定类型字段的文件ID

直接给你两种实现你想要的调用效果的方案,适配Drupal Webform场景:

方案一:扩展WebformSubmission类(推荐)

通过自定义Trait给WebformSubmission对象添加你期望的getFieldDefinitionType方法,实现原生调用的体验:

1. 创建Trait文件

在自定义模块下新建src/Traits/WebformSubmissionFieldTypeTrait.php,内容如下:

<?php

namespace Drupal\your_custom_module\Traits;

use Drupal\webform\WebformSubmissionInterface;

trait WebformSubmissionFieldTypeTrait {

  /**
   * 获取指定类型字段的所有文件ID
   *
   * @param string $field_type
   *  目标字段类型,比如#webform_document_file
   * @return array
   *  键为字段名,值为对应文件ID数组的结果集
   */
  public function getFieldDefinitionType(string $field_type): array {
    $file_ids = [];
    /** @var WebformSubmissionInterface $this */
    $elements = $this->getWebform()->getElementsDecoded();

    foreach ($elements as $field_name => $element) {
      // 匹配目标字段类型
      if (isset($element['#type']) && $element['#type'] === $field_type) {
        $field_value = $this->getElementData($field_name);
        // 提取文件ID,webform_document_file返回带fid的数组结构
        if (!empty($field_value)) {
          $file_ids[$field_name] = array_column($field_value, 'fid');
        }
      }
    }

    return $file_ids;
  }
}

2. 注入Trait到WebformSubmission

在模块的.module文件中添加钩子,替换默认的WebformSubmission类:

<?php

use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Implements hook_entity_type_build().
 */
function your_custom_module_entity_type_build(array &$entity_types) {
  if (isset($entity_types['webform_submission'])) {
    $entity_types['webform_submission']->setClass('Drupal\your_custom_module\Entity\WebformSubmission');
  }
}

接着新建src/Entity/WebformSubmission.php继承原类并引入Trait:

<?php

namespace Drupal\your_custom_module\Entity;

use Drupal\webform\Entity\WebformSubmission as BaseWebformSubmission;
use Drupal\your_custom_module\Traits\WebformSubmissionFieldTypeTrait;

class WebformSubmission extends BaseWebformSubmission {
  use WebformSubmissionFieldTypeTrait;
}

3. 调用方式

在Webform处理程序中直接使用你想要的调用格式:

$webform_submission = $this->getWebformSubmission();
// 完全符合你的期望调用
$file_ids = $webform_submission->getFieldDefinitionType('#webform_document_file');

方案二:使用辅助函数(快速实现)

如果不想扩展实体类,直接写工具函数更省心:

在模块的.module文件中添加:

<?php

/**
 * 获取Webform提交中指定类型字段的所有文件ID
 *
 * @param \Drupal\webform\WebformSubmissionInterface $submission
 *  Webform提交对象
 * @param string $field_type
 *  目标字段类型
 * @return array
 */
function webform_get_type_field_file_ids(\Drupal\webform\WebformSubmissionInterface $submission, string $field_type): array {
  $file_ids = [];
  $elements = $submission->getWebform()->getElementsDecoded();

  foreach ($elements as $field_name => $element) {
    if (isset($element['#type']) && $element['#type'] === $field_type) {
      $field_value = $submission->getElementData($field_name);
      if (!empty($field_value)) {
        $file_ids[$field_name] = array_column($field_value, 'fid');
      }
    }
  }

  return $file_ids;
}

调用方式

$file_ids = webform_get_type_field_file_ids($webform_submission, '#webform_document_file');

注意事项

  • 替换代码中的your_custom_module为你实际的自定义模块名称
  • webform_document_file字段提交值为数组结构,每个元素包含fid,所以用array_column提取ID
  • 若字段允许多文件上传,返回对应字段下的多个ID数组;单文件则返回单个ID的数组

内容的提问来源于stack exchange,提问作者pemson pereira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 23:26:06