function EntityTypeInfo::entityPrepareForm

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/src/EntityTypeInfo.php \Drupal\content_moderation\EntityTypeInfo::entityPrepareForm()
  2. 10 core/modules/content_moderation/src/EntityTypeInfo.php \Drupal\content_moderation\EntityTypeInfo::entityPrepareForm()
  3. 11.x core/modules/content_moderation/src/EntityTypeInfo.php \Drupal\content_moderation\EntityTypeInfo::entityPrepareForm()

Replaces the entity form entity object with a proper revision object.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity being edited.

string $operation: The entity form operation.

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

See also

hook_entity_prepare_form()

File

core/modules/content_moderation/src/EntityTypeInfo.php, line 287

Class

EntityTypeInfo
Manipulates entity type information.

Namespace

Drupal\content_moderation

Code

public function entityPrepareForm(EntityInterface $entity, $operation, FormStateInterface $form_state) {
    
    /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
    $form_object = $form_state->getFormObject();
    if ($this->isModeratedEntityEditForm($form_object) && !$entity->isNew()) {
        // Generate a proper revision object for the current entity. This allows
        // to correctly handle translatable entities having pending revisions.
        
        /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
        $storage = $this->entityTypeManager
            ->getStorage($entity->getEntityTypeId());
        
        /** @var \Drupal\Core\Entity\ContentEntityInterface $new_revision */
        $new_revision = $storage->createRevision($entity, FALSE);
        // Restore the revision ID as other modules may expect to find it still
        // populated. This will reset the "new revision" flag, however the entity
        // object will be marked as a new revision again on submit.
        // @see \Drupal\Core\Entity\ContentEntityForm::buildEntity()
        $revision_key = $new_revision->getEntityType()
            ->getKey('revision');
        $new_revision->set($revision_key, $new_revision->getLoadedRevisionId());
        $form_object->setEntity($new_revision);
    }
}

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