You are here

public function ViewsRevisionsConverter::convert in Config Entity Revisions 8

Same name in this branch
  1. 8 modules/views_revisions/src/ParamConverter/ViewsRevisionsConverter.php \Drupal\views_revisions\ParamConverter\ViewsRevisionsConverter::convert()
  2. 8 modules/views_revisions/src/ProxyClass/ParamConverter/ViewsRevisionsConverter.php \Drupal\views_revisions\ProxyClass\ParamConverter\ViewsRevisionsConverter::convert()
Same name and namespace in other branches
  1. 1.x modules/views_revisions/src/ParamConverter/ViewsRevisionsConverter.php \Drupal\views_revisions\ParamConverter\ViewsRevisionsConverter::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

modules/views_revisions/src/ParamConverter/ViewsRevisionsConverter.php, line 78

Class

ViewsRevisionsConverter
Provides upcasting for a view entity to be used in the Views UI, with revisions support.

Namespace

Drupal\views_revisions\ParamConverter

Code

public function convert($value, $definition, $name, array $defaults) {

  /* @var $entity \Drupal\config_entity_revisions\ConfigEntityRevisionsInterface */
  if (!($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.
  $store = $this->tempStoreFactory
    ->get('views');

  /* @var $revisionsEntity ConfigEntityRevisionsEntityInterface */
  $revisionsEntity = NULL;

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

    /* @var $storage ConfigEntityRevisionsStorageInterface */
    $storage = $this->entityTypeManager
      ->getStorage('config_entity_revisions');
    if (!empty($defaults['revision_id']) && $defaults['revision_id'] != 'default') {
      $revisionsEntity = $storage
        ->loadRevision($defaults['revision_id']);
    }
    elseif (array_key_exists('load_latest_revision', $definition)) {
      $revisionsEntity = $storage
        ->getLatestRevision($revisionsID);
    }
    else {

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

    // Now that we know the revision ID to use, we can check whether we were
    // already editing it.
    $store_key = $value . '-' . $revisionsEntity
      ->getRevisionId();
  }
  else {
    $store_key = $value;
  }
  if ($view = $store
    ->get($store_key)) {
    if ($entity
      ->status()) {
      $view
        ->enable();
    }
    else {
      $view
        ->disable();
    }
    $view->lock = $store
      ->getMetadata($store_key);
  }
  else {
    if ($revisionsEntity) {

      // Otherwise, decorate the existing view for use in the UI.
      $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());
    }
    $view = new ViewsRevisionsUI($entity);
  }
  return $view;
}