You are here

public function EntityModerationForm::submitForm in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 src/Form/EntityModerationForm.php \Drupal\workbench_moderation\Form\EntityModerationForm::submitForm()

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/EntityModerationForm.php, line 136

Class

EntityModerationForm
Defines a form for entity moderation.

Namespace

Drupal\workbench_moderation\Form

Code

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

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $form_state
    ->get('entity');
  $new_state = $form_state
    ->getValue('new_state');
  $entity->moderation_state->target_id = $new_state;
  if ($entity instanceof RevisionLogInterface) {
    $entity
      ->setRevisionLogMessage($form_state
      ->getValue('revision_log'));
    $entity
      ->setRevisionUserId($this
      ->currentUser()
      ->id());
  }
  $entity
    ->save();
  $this
    ->messenger()
    ->addMessage($this
    ->t('The moderation state has been updated.'));

  /** @var \Drupal\workbench_moderation\Entity\ModerationState $state */
  $state = $this->entityTypeManager
    ->getStorage('moderation_state')
    ->load($new_state);

  // The page we're on likely won't be visible if we just set the entity to
  // the default state, as we hide that latest-revision tab if there is no
  // forward revision. Redirect to the canonical URL instead, since that will
  // still exist.
  if ($state
    ->isDefaultRevisionState()) {
    $form_state
      ->setRedirectUrl($entity
      ->toUrl('canonical'));
  }
}