You are here

class ViewsRevisionsConverter in Config Entity Revisions 1.x

Same name in this branch
  1. 1.x modules/views_revisions/src/ParamConverter/ViewsRevisionsConverter.php \Drupal\views_revisions\ParamConverter\ViewsRevisionsConverter
  2. 1.x modules/views_revisions/src/ProxyClass/ParamConverter/ViewsRevisionsConverter.php \Drupal\views_revisions\ProxyClass\ParamConverter\ViewsRevisionsConverter
Same name and namespace in other branches
  1. 8 modules/views_revisions/src/ParamConverter/ViewsRevisionsConverter.php \Drupal\views_revisions\ParamConverter\ViewsRevisionsConverter

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

Example:

pattern: '/some/{view}/{revision_id}/and/{bar}' options: parameters: view: type: 'entity:view' tempstore: TRUE revision_id: \+d

The value for {view} will be converted to a view entity prepared for the Views UI and loaded from the views temp store, but it will not touch the value for {bar}.

This class extends AdminPathConfigEntityConverter rather than ViewUIConverter so that ViewUIConverter's converter can be replaced rather than extended (we call the parent method). Other methods should remain the same as ViewUIConverter.

Hierarchy

Expanded class hierarchy of ViewsRevisionsConverter

1 string reference to 'ViewsRevisionsConverter'
views_revisions.services.yml in modules/views_revisions/views_revisions.services.yml
modules/views_revisions/views_revisions.services.yml
1 service uses ViewsRevisionsConverter
paramconverter.view_revision in modules/views_revisions/views_revisions.services.yml
Drupal\views_revisions\ParamConverter\ViewsRevisionsConverter

File

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

Namespace

Drupal\views_revisions\ParamConverter
View source
class ViewsRevisionsConverter extends AdminPathConfigEntityConverter implements ParamConverterInterface {
  use ViewsRevisionsConfigTrait;

  /**
   * Stores the tempstore factory.
   *
   * @var \Drupal\Core\TempStore\SharedTempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * Constructs a new ViewUIConverter.
   *
   * @param \Drupal\Core\Entity\entityTypeManagerInterface $entity_manager
   *   The entity manager.
   * @param \Drupal\Core\TempStore\SharedTempStoreFactory $temp_store_factory
   *   The factory for the temp store object.
   */
  public function __construct(EntityTypeManagerInterface $entity_manager, SharedTempStoreFactory $temp_store_factory, ConfigFactoryInterface $config_factory = NULL, AdminContext $admin_context = NULL, EntityRepositoryInterface $entityRepository = NULL) {

    // The config factory and admin context are new arguments due to changing
    // the parent. Avoid an error on updated sites by falling back to getting
    // them from the container.
    // @todo Remove in 8.2.x in https://www.drupal.org/node/2674328.
    if (!$config_factory) {
      $config_factory = \Drupal::configFactory();
    }
    if (!$admin_context) {
      $admin_context = \Drupal::service('router.admin_context');
    }
    parent::__construct($entity_manager, $config_factory, $admin_context, $entityRepository);
    $this->tempStoreFactory = $temp_store_factory;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function applies($definition, $name, Route $route) {
    if (parent::applies($definition, $name, $route)) {
      return !empty($definition['tempstore']) || !empty($route
        ->getRequirement('revision_id')) && $definition['type'] === 'entity:view';
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdminPathConfigEntityConverter::$adminContext protected property The route admin context to determine whether a route is an admin one.
AdminPathConfigEntityConverter::$configFactory protected property The config factory.
ConfigEntityRevisionsStorageTrait::admin_permission public function
ConfigEntityRevisionsStorageTrait::config_entity_name public function
ConfigEntityRevisionsStorageTrait::content_entity_type public function
ConfigEntityRevisionsStorageTrait::content_parameter_name public function
ConfigEntityRevisionsStorageTrait::content_parent_reference_field public function
ConfigEntityRevisionsStorageTrait::has_canonical_url public function
ConfigEntityRevisionsStorageTrait::has_own_content public function
ConfigEntityRevisionsStorageTrait::module_name public function
ConfigEntityRevisionsStorageTrait::revisions_entity_name public function
ConfigEntityRevisionsStorageTrait::setting_name public function
ConfigEntityRevisionsStorageTrait::title public function
DynamicEntityTypeParamConverterTrait::getEntityTypeFromDefaults protected function Determines the entity type ID given a route definition and route defaults.
EntityConverter::$entityRepository protected property Entity repository.
EntityConverter::$entityTypeManager protected property Entity type manager which performs the upcasting in the end.
ViewsRevisionsConfigTrait::$constants private property
ViewsRevisionsConfigTrait::revisioned_entity public function Get the entity that actually has revisions.
ViewsRevisionsConverter::$tempStoreFactory protected property Stores the tempstore factory.
ViewsRevisionsConverter::applies public function Determines if the converter applies to a specific route and variable. Overrides AdminPathConfigEntityConverter::applies
ViewsRevisionsConverter::convert public function Converts path variables to their corresponding objects. Overrides AdminPathConfigEntityConverter::convert
ViewsRevisionsConverter::__construct public function Constructs a new ViewUIConverter. Overrides AdminPathConfigEntityConverter::__construct