You are here

public function ConfigEntityRevisionsControllerBase::loadConfigEntityRevision in Config Entity Revisions 8

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

Load a particular revision of a config entity.

Parameters

int $revision: The revision ID to load.

mixed $entity: The entity type to load.

Return value

mixed The loaded revision or NULL.

File

src/ConfigEntityRevisionsControllerBase.php, line 418

Class

ConfigEntityRevisionsControllerBase
Controller to make library functions available to various consumers.

Namespace

Drupal\config_entity_revisions

Code

public function loadConfigEntityRevision($revision = NULL, $entity = '') {
  $config_entity_name = $this
    ->config_entity_name();
  if (!$entity) {
    try {
      $match = \Drupal::service('router')
        ->matchRequest(\Drupal::request());
    } catch (\Exception $exception) {
      $match = [];
    }
    $entity = isset($match[$config_entity_name]) ? $match[$config_entity_name] : NULL;
  }
  if (is_string($entity)) {
    $entity = $this->entityTypeManager
      ->getStorage($config_entity_name)
      ->load($entity);
  }
  if ($revision) {
    $revisionsEntity = $this->entityTypeManager
      ->getStorage('config_entity_revisions')
      ->loadRevision($revision);
    $entity = \Drupal::getContainer()
      ->get('serializer')
      ->deserialize($revisionsEntity
      ->get('configuration')->value, get_class($entity), 'json');

    // The result of serialising and then deserialising is not an exact
    // copy of the original. This causes problems downstream if we don't fix
    // a few attributes here.
    $entity
      ->set('settingsOriginal', $entity
      ->get('settings'));
    $entity
      ->set('enforceIsNew', FALSE);

    // Record the revision ID in the config entity so we can quickly and
    // easily access the revision record if needed (eg for edit form revision
    // message).
    $entity
      ->updateLoadedRevisionId($revisionsEntity
      ->getRevisionId());
  }
  return $entity;
}