public function LayoutParagraphsWidget::saveItemSubmit in Layout Paragraphs 1.0.x
Form submit handler - saves an item.
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 1626
Class
- LayoutParagraphsWidget
- Entity Reference with Layout field widget.
Namespace
Drupal\layout_paragraphs\Plugin\Field\FieldWidgetCode
public function saveItemSubmit(array $form, FormStateInterface $form_state) {
$element = $form_state
->getTriggeringElement();
$parents = $element['#element_parents'];
$delta = $element['#delta'];
$element_array_parents = $element['#array_parents'];
$item_array_parents = array_splice($element_array_parents, 0, -2);
$item_form = NestedArray::getValue($form, $item_array_parents);
/* @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */
$display = $item_form['#display'];
$widget_state = static::getWidgetState($parents, $this->fieldName, $form_state);
// Remove is_new flag since we're saving the entity.
unset($widget_state['items'][$delta]['is_new']);
/** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
$paragraph = $widget_state['items'][$delta]['entity'];
// Set correct default language for the entity.
if ($this->isTranslating && ($language = $form_state
->get('langcode'))) {
$paragraph = $paragraph
->getTranslation($language);
}
// Save field values to entity.
$display
->extractFormValues($paragraph, $item_form, $form_state);
// Submit behavior forms.
$paragraphs_type = $paragraph
->getParagraphType();
if ($this->currentUser
->hasPermission('edit behavior plugin settings')) {
foreach ($paragraphs_type
->getEnabledBehaviorPlugins() as $plugin_id => $plugin_values) {
$plugin_form = isset($item_form['behavior_plugins']) ? $item_form['behavior_plugins'][$plugin_id] : [];
if (!empty($plugin_form) && !empty(Element::children($plugin_form))) {
$subform_state = SubformState::createForSubform($item_form['behavior_plugins'][$plugin_id], $form_state
->getCompleteForm(), $form_state);
$plugin_values
->submitBehaviorForm($paragraph, $item_form['behavior_plugins'][$plugin_id], $subform_state);
}
}
}
// Save paragraph back to widget state.
$widget_state['items'][$delta]['entity'] = $paragraph;
// Save layout settings.
if (!empty($item_form['layout_selection']['layout'])) {
$layout_settings = $this
->getLayoutSettings($paragraph);
$layout = $form_state
->getValue($item_form['layout_selection']['layout']['#parents']);
$layout_settings['layout'] = $layout;
// Save layout config:
if (!empty($item_form['layout_plugin_form'])) {
try {
$layout_instance = $this->layoutPluginManager
->createInstance($layout);
if ($this
->getLayoutPluginForm($layout_instance)) {
$subform_state = SubformState::createForSubform($item_form['layout_plugin_form'], $form_state
->getCompleteForm(), $form_state);
$layout_instance
->submitConfigurationForm($item_form['layout_plugin_form'], $subform_state);
$layout_settings['config'] = $layout_instance
->getConfiguration();
}
$this
->setLayoutSettings($paragraph, $layout_settings);
} catch (\Exception $e) {
watchdog_exception('Layout Paragraphs, Layout Instance generation', $e);
}
}
// Handle orphaned items.
if (isset($item_form['layout_selection']['move_items'])) {
$move_items = $form_state
->getValue($item_form['layout_selection']['move_items']['#parents']);
if ($move_items && isset($move_items['items'])) {
$parent_uuid = $paragraph
->uuid();
foreach ($move_items['items'] as $from_region => $to_region) {
foreach ($widget_state['items'] as $delta => $item) {
$layout_settings = $this
->getLayoutSettings($item['entity']);
if ($layout_settings['parent_uuid'] == $parent_uuid && $layout_settings['region'] == $from_region) {
$this
->setLayoutSetting($widget_state['items'][$delta]['entity'], 'region', $to_region);
// We have to update user input directly
// or the region setting will be
// overwritten by the form.
$path = array_merge($parents, [
$this->fieldName,
$delta,
'region',
]);
$input = $form_state
->getUserInput();
NestedArray::setValue($input, $path, $to_region);
$form_state
->setUserInput($input);
}
}
}
}
}
}
// Close the entity form.
$widget_state['open_form'] = FALSE;
static::setWidgetState($parents, $this->fieldName, $form_state, $widget_state);
$form_state
->setRebuild();
}