【问题标题】:Conditionally change field widget type with Drupal hook_form_alter使用 Drupal hook_form_alter 有条件地更改字段小部件类型
【发布时间】:2019-09-09 14:27:36
【问题描述】:

我想在 Drupal 8.7.6 中使用 hook_form_alter 有条件地更改附加到节点的纯文本字段的小部件。

function mymodule_form_node_article_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $options=[
    'test1'  => 'AAA',
    'test2'  => 'BBB',
    'test3'  => 'CCC',
    'test4'  => 'DDD',
  ];
  $form['field_testdrop']['widget'][0]['#type']='select';
  $form['field_testdrop']['widget'][0]['#options'] = $options;

}

但是,在尝试保存节点时,我收到以下错误:

错误:无法在 Drupal\Component\Utility\NestedArray::setValue()(core/lib/Drupal/Component/Utility/NestedArray.php 的第 155 行)中创建指向/来自字符串偏移的引用。

我能做些什么来防止这种情况发生?

【问题讨论】:

    标签: widget drupal-8 hook-form-alter


    【解决方案1】:

    这个作品:

        $form['field_priority_email_id']['widget'][0]['value']['#type'] = 'select';
        $form['field_priority_email_id']['widget'][0]['value']['#options'] = $options;
        $form['field_priority_email_id']['widget'][0]['value']['#size'] = NULL;
    

    您必须将“#size”设置为 NULL,否则选择小部件将无法正确呈现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      相关资源
      最近更新 更多