You are here

public function ConfigEntityRevisionsControllerBase::createInitialRevision in Config Entity Revisions 8

Same name and namespace in other branches
  1. 1.x src/ConfigEntityRevisionsControllerBase.php \Drupal\config_entity_revisions\ConfigEntityRevisionsControllerBase::createInitialRevision()

Create an initial revision record.

Parameters

ConfigEntityRevisionsInterface $config_entity: The configuration entity.

Return value

ContentEntityInterface|NULL The content entity created.

Throws

\Drupal\Core\Entity\EntityStorageException

Overrides ConfigEntityRevisionsControllerInterface::createInitialRevision

File

src/ConfigEntityRevisionsControllerBase.php, line 145

Class

ConfigEntityRevisionsControllerBase
Controller to make library functions available to various consumers.

Namespace

Drupal\config_entity_revisions

Code

public function createInitialRevision(ConfigEntityRevisionsInterface $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
    ->configEntityStorage()
    ->load($config_entity
    ->id());
  $source = $originalEntity ? $originalEntity : $config_entity;
  $bundle_type = $config_entity
    ->getEntityTypeId() . "_revisions";
  $user_id = $this->container
    ->get('current_user')
    ->id();
  $request_time = $this->container
    ->get('datetime.time')
    ->getRequestTime();

  /* @var $contentEntity ContentEntityInterface */
  $contentEntity = $config_entity
    ->contentEntityStorage()
    ->create([
    'form' => $source
      ->get('uuid'),
    'configuration' => $this->serialiser
      ->serialize($source, 'json'),
    'type' => $bundle_type,
  ]);

  // Set revision info.
  $contentEntity
    ->setNewRevision(TRUE);
  $contentEntity->revision_log = 'Original revision';
  $contentEntity
    ->setRevisionCreationTime($request_time);
  $contentEntity
    ->setRevisionUserId($user_id);
  $contentEntity->moderation_state->value = 'draft';
  $contentEntity
    ->save();
  $contentID = $contentEntity
    ->id();
  $config_entity
    ->setContentEntityID($contentID);
  $config_entity
    ->save();
  return $contentEntity;
}