You are here

public function LayoutParagraphsWidget::extractFormValues in Layout Paragraphs 2.0.x

Extracts field values from submitted form values.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values. This parameter is altered by reference to receive the incoming form values.

array $form: The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Overrides WidgetBase::extractFormValues

File

src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php, line 165

Class

LayoutParagraphsWidget
Layout paragraphs widget.

Namespace

Drupal\layout_paragraphs\Plugin\Field\FieldWidget

Code

public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
  $field_name = $this->fieldDefinition
    ->getName();
  $path = array_merge($form['#parents'], [
    $field_name,
  ]);
  $layout_paragraphs_layout = $this->tempstore
    ->get(new LayoutParagraphsLayout($items));
  $values = [];
  foreach ($layout_paragraphs_layout
    ->getParagraphsReferenceField() as $item) {
    if ($item->entity) {
      $entity = $item->entity;
      $entity
        ->setNeedsSave(TRUE);
      $values[] = [
        'entity' => $entity,
        'target_id' => $entity
          ->id(),
        'target_revision_id' => $entity
          ->getRevisionId(),
      ];
    }
  }
  $form_state
    ->setValue($path, $values);
  return parent::extractFormValues($items, $form, $form_state);
}