You are here

public function ModerationStateWidget::extractFormValues in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/ModerationStateWidget.php \Drupal\workbench_moderation\Plugin\Field\FieldWidget\ModerationStateWidget::extractFormValues()

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/ModerationStateWidget.php, line 254

Class

ModerationStateWidget
Plugin implementation of the 'moderation_state_default' widget.

Namespace

Drupal\workbench_moderation\Plugin\Field\FieldWidget

Code

public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
  $field_name = $this->fieldDefinition
    ->getName();

  // Extract the values from $form_state->getValues().
  $path = array_merge($form['#parents'], [
    $field_name,
  ]);
  $key_exists = NULL;

  // Convert the field value into expected array format.
  $values = $form_state
    ->getValues();
  $value = NestedArray::getValue($values, $path, $key_exists);
  if (empty($value)) {
    parent::extractFormValues($items, $form, $form_state);
    return;
  }
  if (!isset($value[0]['target_id'])) {
    NestedArray::setValue($values, $path, [
      [
        'target_id' => reset($value),
      ],
    ]);
    $form_state
      ->setValues($values);
  }
  parent::extractFormValues($items, $form, $form_state);
}