我想通过使用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行)中创建对字符串偏移量的引用/来自字符串偏移量。
我该怎么做才能防止这种情况?
答案 0 :(得分:0)
这有效:
$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,否则select小部件将无法正确呈现。