public function LayoutParagraphsWidget::newItemSubmit in Layout Paragraphs 1.0.x
Form submit handler - adds a new item and opens its edit form.
Parameters
array $form: The form array.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
File
- src/
Plugin/ Field/ FieldWidget/ LayoutParagraphsWidget.php, line 1486
Class
- LayoutParagraphsWidget
- Entity Reference with Layout field widget.
Namespace
Drupal\layout_paragraphs\Plugin\Field\FieldWidgetCode
public function newItemSubmit(array $form, FormStateInterface $form_state) {
$element = $form_state
->getTriggeringElement();
$parents = $element['#element_parents'];
$widget_state = static::getWidgetState($parents, $this->fieldName, $form_state);
if (!empty($element['#bundle_id'])) {
$bundle_id = $element['#bundle_id'];
}
else {
$element_parents = $element['#parents'];
array_splice($element_parents, -1, 1, 'type');
$bundle_id = $form_state
->getValue($element_parents);
}
try {
$entity_type = $this->entityTypeManager
->getDefinition('paragraph');
$bundle_key = $entity_type
->getKey('bundle');
/** @var \Drupal\paragraphs\ParagraphInterface $paragraph_entity */
$paragraph_entity = $this->entityTypeManager
->getStorage('paragraph')
->create([
$bundle_key => $bundle_id,
]);
$paragraph_entity
->setParentEntity($element['#host'], $this->fieldDefinition
->getName());
$widget_state['items'][] = [
'entity' => $paragraph_entity,
'is_new' => TRUE,
'weight' => count($widget_state['items']),
];
$widget_state['open_form'] = $widget_state['items_count'];
$widget_state['items_count'] = count($widget_state['items']);
static::setWidgetState($parents, $this->fieldName, $form_state, $widget_state);
$form_state
->setRebuild();
} catch (\Exception $e) {
watchdog_exception('Layout Paragraphs, new Item Submit', $e);
}
}