You are here

public function ConfigEntityRevisionsRevisionStorageHandler::createInitialRevision in Config Entity Revisions 8.2

Create an initial revision record.

Parameters

\Drupal\config_entity_revisions\ConfigEntityRevisionsConfigEntityInterface $config_entity: The configuration entity.

Return value

\Drupal\Core\Entity\ContentEntityInterface|null The content entity created.

Throws

\Drupal\Core\Entity\EntityStorageException

Overrides ConfigEntityRevisionsRevisionStorageHandlerInterface::createInitialRevision

File

src/Entity/Handler/ConfigEntityRevisionsRevisionStorageHandler.php, line 291

Class

ConfigEntityRevisionsRevisionStorageHandler
Class ConfigEntityRevisionsRevisionStorageHandler.

Namespace

Drupal\config_entity_revisions\Entity\Handler

Code

public function createInitialRevision(ConfigEntityRevisionsConfigEntityInterface $config_entity) {
  $contentID = $config_entity
    ->getContentEntityID();

  // Already created.
  if ($contentID) {
    return NULL;
  }

  // Make a content revisions entity using either the previous version of
  // the config entity or (failing that) the current version.
  // We're doing this here rather than in the update hook because we want
  // to save the reference to the entity config entity version that is being
  // saved now.

  /* @var $originalEntity ConfigEntityInterface */
  $originalEntity = $config_entity
    ->contentEntityStorage()
    ->load($config_entity
    ->id());
  $source = $originalEntity ? $originalEntity : $config_entity;
  $bundle_type = $config_entity
    ->getBundleName();

  /* @var $contentEntity ContentEntityInterface */
  $contentEntity = $config_entity
    ->contentEntityStorage()
    ->create([
    'form' => $source
      ->get('uuid'),
    'configuration' => $this->serialiser
      ->serialize($source, 'json'),
    'revision_user' => $this->currentUser
      ->id(),
    'revision_creation_time' => $this->dateTimeService
      ->getRequestTime(),
    'revision_log_message' => 'Original revision.',
    'moderation_state' => 'draft',
    'type' => $bundle_type,
  ]);
  $contentEntity
    ->save();
  $contentID = $contentEntity
    ->id();
  $config_entity
    ->setContentEntityID($contentID);
  $config_entity
    ->save();
  return $contentEntity;
}