You are here

public function ConfigEntityRevisionsEntityTypeInfo::entityPrepareForm in Config Entity Revisions 8.2

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

src/ConfigEntityRevisionsEntityTypeInfo.php, line 88

Class

ConfigEntityRevisionsEntityTypeInfo
Class ConfigEntityRevisionsEntityTypeInfo.

Namespace

Drupal\config_entity_revisions

Code

public function entityPrepareForm(EntityInterface $entity, $operation, FormStateInterface $form_state) {
  if (!$entity instanceof ConfigEntityRevisionsConfigEntityContainerInterface) {
    parent::entityPrepareForm($entity, $operation, $form_state);
    return;
  }

  /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
  $form_object = $form_state
    ->getFormObject();
  if (!($this
    ->isModeratedEntityEditForm($form_object) && !$entity
    ->isNew())) {
    return;
  }

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