class ViewsRevisionsConverter in Config Entity Revisions 8
Same name in this branch
- 8 modules/views_revisions/src/ParamConverter/ViewsRevisionsConverter.php \Drupal\views_revisions\ParamConverter\ViewsRevisionsConverter
- 8 modules/views_revisions/src/ProxyClass/ParamConverter/ViewsRevisionsConverter.php \Drupal\views_revisions\ProxyClass\ParamConverter\ViewsRevisionsConverter
Same name and namespace in other branches
- 1.x 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
- class \Drupal\Core\ParamConverter\EntityConverter implements ParamConverterInterface uses DeprecatedServicePropertyTrait, DynamicEntityTypeParamConverterTrait
- class \Drupal\Core\ParamConverter\AdminPathConfigEntityConverter
- class \Drupal\views_revisions\ParamConverter\ViewsRevisionsConverter implements ParamConverterInterface uses ViewsRevisionsConfigTrait
- class \Drupal\Core\ParamConverter\AdminPathConfigEntityConverter
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
File
- modules/
views_revisions/ src/ ParamConverter/ ViewsRevisionsConverter.php, line 40
Namespace
Drupal\views_revisions\ParamConverterView 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;
}
}