You are here

public function ModerationStateWidget::extractFormValues in Lightning Workflow 8.3

Same name and namespace in other branches
  1. 8.2 modules/lightning_scheduler/src/Plugin/Field/FieldWidget/ModerationStateWidget.php \Drupal\lightning_scheduler\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

modules/lightning_scheduler/src/Plugin/Field/FieldWidget/ModerationStateWidget.php, line 242

Class

ModerationStateWidget
Scheduler extension of Content Moderation's widget.

Namespace

Drupal\lightning_scheduler\Plugin\Field\FieldWidget

Code

public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
  parent::extractFormValues($items, $form, $form_state);
  $transitions = $form_state
    ->getValue('transition_storage');
  $entity = $items
    ->getEntity();
  $uuid = $entity
    ->uuid();

  // Do not use empty() here, because it's possible that the user is trying to
  // clear all scheduled transitions, which means $transitions[$uuid] will
  // be an empty array.
  if (!isset($transitions[$uuid])) {
    return;
  }
  $states = array_map(function (array $transition) {
    if ($transition['state'] && is_string($transition['state'])) {
      return [
        'value' => $transition['state'],
      ];
    }
    else {
      throw new \InvalidArgumentException('Invalid transition state: ' . $transition['state']);
    }
  }, $transitions[$uuid]);
  $dates = array_map(function (array $transition) {
    return [
      'value' => gmdate(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $transition['when']),
    ];
  }, $transitions[$uuid]);
  if (count($states) === count($dates)) {
    $entity
      ->set('scheduled_transition_state', $states)
      ->set('scheduled_transition_date', $dates);
  }
  else {
    throw new \LogicException('scheduled_transition_state and scheduled_transition_date fields must have the same number of items.');
  }
}