function EntityModerationForm::submitForm

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/src/Form/EntityModerationForm.php \Drupal\content_moderation\Form\EntityModerationForm::submitForm()
  2. 8.9.x core/modules/content_moderation/src/Form/EntityModerationForm.php \Drupal\content_moderation\Form\EntityModerationForm::submitForm()
  3. 10 core/modules/content_moderation/src/Form/EntityModerationForm.php \Drupal\content_moderation\Form\EntityModerationForm::submitForm()

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'));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.