You are here

public function ConfigEntityRevisionsControllerBase::createUpdateRevision in Config Entity Revisions 8

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

Create revision when a new config entity version is saved.

Parameters

ConfigEntityRevisionsInterface $config_entity: The configuration entity.

Throws

\Drupal\Core\Entity\EntityStorageException

Overrides ConfigEntityRevisionsControllerInterface::createUpdateRevision

File

src/ConfigEntityRevisionsControllerBase.php, line 202

Class

ConfigEntityRevisionsControllerBase
Controller to make library functions available to various consumers.

Namespace

Drupal\config_entity_revisions

Code

public function createUpdateRevision(ConfigEntityRevisionsInterface $config_entity) {

  /* @var $revisionsEntity \Drupal\config_entity_revisions\ConfigEntityRevisionsEntityInterface */
  $revisionsEntity = NULL;
  $previous_state = FALSE;
  if (!empty($config_entity
    ->getRevisionId())) {
    $revisionsEntity = $config_entity
      ->contentEntityStorage()
      ->loadRevision($config_entity
      ->getRevisionID());
    $previous_state = $revisionsEntity->moderation_state->value;
  }
  else {
    $contentID = $config_entity
      ->getContentEntityID();
    if (is_null($contentID)) {

      // No related content entity yet.
      return;
    }
    $revisionsEntity = $config_entity
      ->contentEntityStorage()
      ->load($contentID);
  }
  $revisionsEntity
    ->set('configuration', $this->serialiser
    ->serialize($config_entity, 'json'));
  $revisionsEntity
    ->setRevisionUserId($this->currentUser
    ->id());
  $revisionsEntity
    ->setRevisionCreationTime($this->container
    ->get('datetime.time')
    ->getRequestTime());
  $new_message = $config_entity
    ->get('revision_log_message')[0]['value'];
  $new_revision = $config_entity
    ->get('revision');
  $moderation_state = is_null($config_entity
    ->get('moderation_state')) ? null : $config_entity
    ->get('moderation_state')[0]['value'];
  $published = NULL;
  if (!is_null($moderation_state)) {
    $published = $moderation_state == 'published';
  }
  if (is_null($moderation_state) && is_null($new_revision)) {
    $new_revision = FALSE;
  }
  if (!is_null($new_message)) {
    $revisionsEntity
      ->setRevisionLogMessage($config_entity
      ->get('revision_log_message')[0]['value']);
  }
  $revisionsEntity
    ->setNewRevision($new_revision);
  if (!is_null($moderation_state)) {
    $revisionsEntity->moderation_state = $moderation_state;
  }
  if (!is_null($published)) {
    if ($published) {
      $revisionsEntity
        ->setPublished();
    }
    else {
      $revisionsEntity
        ->setUnpublished();
    }
    $revisionsEntity
      ->isDefaultRevision($published);
  }
  $revisionsEntity
    ->save();
  if (($previous_state == 'published') !== $published) {

    // Modify another revision to be published and default if possible.
    $this
      ->resetDefaultRevision($revisionsEntity);
  }
}