我在表单中添加了一个自定义按钮:
$form['actions']['autotag_content'] = [
'#type' => 'button',
'#value' => 'Autotag Content',
'#ajax' => [
'callback' => ['\Drupal\taxonomy_migrate\taggerService', 'tagContent'],
'wrapper' => ['block-adminimal-theme-content'],
'progress' => [
'type' => 'throbber',
'message' => 'Tagging content',
],
],
];
然后在回调中,我想从表单上的实体引用字段中添加或删除实体。然后,这将被发送回浏览器并重新呈现。我不希望保存更改,我只想将它们填充到表单中,然后用户可以接受更改。
为了这个例子,我已经简化了这个只是为了证明这一点。我想向 field_tax_subjects 添加两个实体引用并重新呈现前端表单。目前,前端表单重新呈现,但不反映更改
public static function tagContent(array &$form, FormStateInterface &$form_state) {
$node = $form_state->getFormObject()->getEntity();
$node->field_tax_subjects[] = 12345;
$node->field_tax_subjects[] = 23456;
$form = \Drupal::service('entity.form_builder')->getForm($node);
$form_state->setRebuild();
return $form;
}