You are here

public function ConfigEntityRevisionsConverterBase::convert in Config Entity Revisions 8

Same name and namespace in other branches
  1. 8.2 src/ConfigEntityRevisionsConverterBase.php \Drupal\config_entity_revisions\ConfigEntityRevisionsConverterBase::convert()
  2. 1.x src/ConfigEntityRevisionsConverterBase.php \Drupal\config_entity_revisions\ConfigEntityRevisionsConverterBase::convert()

Converts path variables to their corresponding objects.

Parameters

mixed $value: The raw value.

mixed $definition: The parameter definition provided in the route options.

string $name: The name of the parameter.

array $defaults: The route defaults array.

Return value

mixed|null The converted parameter value.

Overrides EntityConverter::convert

File

src/ConfigEntityRevisionsConverterBase.php, line 67

Class

ConfigEntityRevisionsConverterBase
Parameter converter for upcasting entity IDs to full objects.

Namespace

Drupal\config_entity_revisions

Code

public function convert($value, $definition, $name, array $defaults) {
  $entity_type_id = $this
    ->getEntityTypeFromDefaults($definition, $name, $defaults);
  $storage = $this->entityTypeManager
    ->getStorage($entity_type_id);

  /* @var $entity \Drupal\config_entity_revisions\ConfigEntityRevisionsInterface */
  $entity = $storage
    ->load($value);
  if (!$entity) {
    return NULL;
  }

  // Get the config_entity_revisions entity, if one exists.
  $revisionsID = $entity
    ->getContentEntityID();
  if (!$revisionsID) {
    return $entity;
  }

  /* @var $storage ConfigEntityRevisionsStorageInterface */
  $storage = $this->entityTypeManager
    ->getStorage('config_entity_revisions');

  // If a specific revision is provided or implied by a submission ID, use it.
  $specific_revision = FALSE;
  if (!empty($defaults['revision_id'])) {
    $specific_revision = $defaults['revision_id'];
  }
  elseif ($entity
    ->has_own_content() && !empty($defaults[$entity
    ->revisions_entity_name()])) {

    // Load the content entity and get the config entity revision from it (if any).
    $content_storage = $this->entityTypeManager
      ->getStorage($entity
      ->revisions_entity_name());
    $content = $content_storage
      ->load($defaults[$entity
      ->content_parameter_name()]);
    if ($content->{$entity
      ->content_parent_reference_field()}) {
      $specific_revision = $content->{$entity
        ->content_parent_reference_field()}->target_id;
    }
  }

  // If no specific revision has been given, check to see whether we should
  // use the latest revision or the latest published revision.

  /* @var $revisionsEntity ConfigEntityRevisionsEntityInterface */
  $revisionsEntity = NULL;
  if ($specific_revision) {
    $revisionsEntity = $storage
      ->loadRevision($specific_revision);
  }
  elseif (array_key_exists('load_latest_revision', $definition)) {
    $revisionsEntity = $storage
      ->getLatestRevision($revisionsID);
  }
  else {
    $revisionsEntity = $storage
      ->getLatestPublishedRevision($revisionsID);

    // If there's no latest published revision and the user has admin
    // permissions, get the latest revision instead.
    if (\Drupal::currentUser()
      ->hasPermission($entity
      ->admin_permission()) && is_null($revisionsEntity)) {
      $revisionsEntity = $storage
        ->getLatestRevision($revisionsID);
    }
  }
  if (is_null($revisionsEntity)) {
    return NULL;
  }
  $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;
}