简体   繁体   English

如何在AJAX处理程序Drupal 8中以编程方式为Paragraph字段创建新的小部件

[英]How to create a new widget for a Paragraph field programmatically in an AJAX handler Drupal 8

I need to add Paragraph fields programmatically to an entity in an AJAX handler. 我需要以编程方式将“段落”字段添加到AJAX处理程序中的实体。

This is my AJAX callback: 这是我的AJAX回调:

function myAjaxCallback(array &$form, \Drupal\Core\Form\FormState $form_state)  {
  $paragraph3 = \Drupal\paragraphs\Entity\Paragraph::create([
    'type' => 'media',
    'field_caption' => 'Apart from the included Paragraph types, you can create your own simply by going to Structure -> Paragraphs types.',
  ]);
  $paragraph3->save();

  // Now I need to add $paragraph3 to $form['field_gallery_items']['widget']

  return $form;
}

I need to add the new Paragraph I created to the widget so that when the $form is returned I have added a paragraph. 我需要将我创建的新段落添加到小部件中,以便在返回$ form时添加一个段落。

How Can I do that? 我怎样才能做到这一点?

尝试将段落追加到表单字段

$form['field_gallery_items']['widget']->appendItem($paragraph3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM