You are here

public function EntityModerationForm::submitForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/content_moderation/src/Form/EntityModerationForm.php \Drupal\content_moderation\Form\EntityModerationForm::submitForm()
  2. 10 core/modules/content_moderation/src/Form/EntityModerationForm.php \Drupal\content_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

core/modules/content_moderation/src/Form/EntityModerationForm.php, line 142

Class

EntityModerationForm
The EntityModerationForm provides a simple UI for changing moderation state.

Namespace

Drupal\content_moderation\Form

Code

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

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

  /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
  $storage = \Drupal::entityTypeManager()
    ->getStorage($entity
    ->getEntityTypeId());
  $entity = $storage
    ->createRevision($entity, $entity
    ->isDefaultRevision());
  $new_state = $form_state
    ->getValue('new_state');
  $entity
    ->set('moderation_state', $new_state);
  if ($entity instanceof RevisionLogInterface) {
    $entity
      ->setRevisionCreationTime($this->time
      ->getRequestTime());
    $entity
      ->setRevisionLogMessage($form_state
      ->getValue('revision_log'));
    $entity
      ->setRevisionUserId($this
      ->currentUser()
      ->id());
  }
  $entity
    ->save();
  $this
    ->messenger()
    ->addStatus($this
    ->t('The moderation state has been updated.'));
  $new_state = $this->moderationInfo
    ->getWorkflowForEntity($entity)
    ->getTypePlugin()
    ->getState($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
  // pending revision. Redirect to the canonical URL instead, since that will
  // still exist.
  if ($new_state
    ->isDefaultRevisionState()) {
    $form_state
      ->setRedirectUrl($entity
      ->toUrl('canonical'));
  }
}