You are here

public function ConfigEntityRevisionsConverterBase::convert in Config Entity Revisions 8.2

Same name and namespace in other branches
  1. 8 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 AdminPathConfigEntityConverter::convert

File

src/ConfigEntityRevisionsConverterBase.php, line 119

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) {

  /* @var $config_entity \Drupal\config_entity_revisions\ConfigEntityRevisionsConfigEntityInterface */
  if (!($config_entity = parent::convert($value, $definition, $name, $defaults))) {
    return;
  }

  // Get the temp store for this variable if it needs one. Attempt to load the
  // view from the temp store, synchronize its status with the existing view,
  // and store the lock metadata.
  $tempstorePrefix = $this
    ->tempstorePrefix();
  $temp_store = $tempstorePrefix ? $this->tempStoreFactory
    ->get($tempstorePrefix) : NULL;
  if (!$config_entity) {
    return NULL;
  }

  /* @var $revisionEntity ConfigEntityRevisionsConfigEntityInterface */
  $revisionEntity = NULL;

  // Get the config_entity_revisions entity, if one exists.
  $revisionsID = $config_entity
    ->getContentEntityId();

  /* @var $content_entity_storage ConfigEntityRevisionsRevisionStorageHandlerInterface */
  $content_entity_storage = $this->entityManager
    ->getStorage('config_entity_revisions');
  if (!$revisionsID) {
    $revisionsID = $content_entity_storage
      ->createInitialRevision($config_entity);
  }
  $content_entity_storage
    ->setConfigEntity($config_entity);

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

    // Load the content entity and get the config entity revision from it
    // (if any).
    $content_storage = $this->entityManager
      ->getStorage($config_entity
      ->revisionsEntityName());
    $content = $content_storage
      ->load($defaults[$config_entity
      ->contentParameterName()]);
    if ($content->{$config_entity
      ->contentParentReferenceField()}) {
      $specific_revision = $content->{$config_entity
        ->contentParentReferenceField()}->target_id;
    }
  }

  // If no specific revision has been given, check to see whether we should
  // use the latest revision or the latest published revision.
  if ($specific_revision) {
    $revisionEntity = $content_entity_storage
      ->loadRevision($specific_revision);
  }
  elseif (array_key_exists('load_latest_revision', $definition)) {
    $revisionEntity = $content_entity_storage
      ->getLatestRevision();
  }
  else {
    $revisionEntity = $content_entity_storage
      ->getLatestPublishedRevision();

    // If there's no latest published revision and the user has admin
    // permissions, get the latest revision instead.
    if (!$revisionEntity && \Drupal::currentUser()
      ->hasPermission($config_entity
      ->adminPermission()) && is_null($revisionEntity)) {
      $revisionEntity = $content_entity_storage
        ->getLatestRevision();
    }
  }
  if (is_null($revisionEntity)) {
    return NULL;
  }

  // Now that we know the revision ID to use, we can check whether we were
  // already editing it.
  $temp_store_key = $value . '-' . $revisionEntity
    ->getRevisionId();
  if ($temp_store && ($container = $temp_store
    ->get($temp_store_key))) {
    if ($config_entity
      ->status()) {
      $container
        ->enable();
    }
    else {
      $container
        ->disable();
    }
    $container->lock = $temp_store
      ->getMetadata($temp_store_key);
  }
  else {
    if ($revisionEntity) {

      // Otherwise, decorate the existing view for use in the UI.
      $config_entity = $content_entity_storage
        ->getConfigEntity($revisionEntity);
    }
    $container = $this
      ->containerFor($config_entity);
  }
  return $container;
}