You are here

public function QuickTransitionForm::submitForm in Moderation Sidebar 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides FormInterface::submitForm

File

src/Form/QuickTransitionForm.php, line 226

Class

QuickTransitionForm
The QuickTransitionForm provides quick buttons for changing transitions.

Namespace

Drupal\moderation_sidebar\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $form_state
    ->get('entity');

  /** @var \Drupal\content_moderation\Entity\ContentModerationStateInterface[] $transitions */
  $transitions = $this->validation
    ->getValidTransitions($entity, $this
    ->currentUser());
  $element = $form_state
    ->getTriggeringElement();

  /** @var \Drupal\content_moderation\ContentModerationState $state */
  $state = $transitions[$element['#id']]
    ->to();
  $state_id = $state
    ->id();
  if ($form_state
    ->getValue('revision_log_toggle')) {
    $revision_log = $form_state
      ->getValue('revision_log');
  }
  else {
    $revision_log = $this
      ->t('Used the Moderation Sidebar to change the state to "@state".', [
      '@state' => $state
        ->label(),
    ]);
  }
  $revision = $this
    ->prepareNewRevision($entity, $revision_log);
  $revision
    ->set('moderation_state', $state_id);
  $revision
    ->save();
  $this
    ->messenger()
    ->addMessage($this
    ->t('The moderation state has been updated.'));
  if (!$this->moderationInformation
    ->hasPendingRevision($entity)) {
    $form_state
      ->setRedirectUrl($entity
      ->toUrl());
  }
  else {
    $form_state
      ->setRedirectUrl($entity
      ->toUrl('latest-version'));
  }
}