You are here

public function EntityReferenceLayoutWidget::saveItemSubmit in Entity Reference with Layout 8

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/EntityReferenceLayoutWidget.php, line 1135

Class

EntityReferenceLayoutWidget
Entity Reference with Layout field widget.

Namespace

Drupal\entity_reference_layout\Plugin\Field\FieldWidget

Code

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']);

  // Set correct default language for the paragraph.
  $paragraph = $widget_state['items'][$delta]['entity'];
  $paragraph
    ->set('langcode', $form_state
    ->get('langcode'));

  // 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 = $form_state
      ->getValue($item_form['layout_selection']['layout']['#parents']);
    $widget_state['items'][$delta]['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_config = $layout_instance
            ->getConfiguration();
          $widget_state['items'][$delta]['config'] = $layout_config;
        }
      } catch (\Exception $e) {
        watchdog_exception('Erl, Layout Instance generation', $e);
      }
    }
  }

  // Save layout options (deprecated).
  if (!empty($item_form['options'])) {
    $options_path = array_merge($parents, [
      $this->fieldName,
      $delta,
      'entity_form',
      'options',
    ]);
    $widget_state['items'][$delta]['options']['options'] = $form_state
      ->getValue($options_path);
  }

  // Close the entity form.
  $widget_state['open_form'] = FALSE;
  static::setWidgetState($parents, $this->fieldName, $form_state, $widget_state);
  $form_state
    ->setRebuild();
}