annotate core/modules/views_ui/src/ParamConverter/ViewUIConverter.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\views_ui\ParamConverter;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@18 6 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@0 7 use Drupal\Core\ParamConverter\AdminPathConfigEntityConverter;
Chris@18 8 use Drupal\Core\ParamConverter\ParamConverterInterface;
Chris@0 9 use Drupal\Core\Routing\AdminContext;
Chris@14 10 use Drupal\Core\TempStore\SharedTempStoreFactory;
Chris@0 11 use Drupal\views_ui\ViewUI;
Chris@18 12 use Symfony\Component\Routing\Route;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Provides upcasting for a view entity to be used in the Views UI.
Chris@0 16 *
Chris@0 17 * Example:
Chris@0 18 *
Chris@0 19 * pattern: '/some/{view}/and/{bar}'
Chris@0 20 * options:
Chris@0 21 * parameters:
Chris@0 22 * view:
Chris@0 23 * type: 'entity:view'
Chris@0 24 * tempstore: TRUE
Chris@0 25 *
Chris@0 26 * The value for {view} will be converted to a view entity prepared for the
Chris@0 27 * Views UI and loaded from the views temp store, but it will not touch the
Chris@0 28 * value for {bar}.
Chris@0 29 */
Chris@0 30 class ViewUIConverter extends AdminPathConfigEntityConverter implements ParamConverterInterface {
Chris@0 31
Chris@0 32 /**
Chris@0 33 * Stores the tempstore factory.
Chris@0 34 *
Chris@14 35 * @var \Drupal\Core\TempStore\SharedTempStoreFactory
Chris@0 36 */
Chris@0 37 protected $tempStoreFactory;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * Constructs a new ViewUIConverter.
Chris@0 41 *
Chris@18 42 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@18 43 * The entity type manager.
Chris@14 44 * @param \Drupal\Core\TempStore\SharedTempStoreFactory $temp_store_factory
Chris@0 45 * The factory for the temp store object.
Chris@18 46 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@18 47 * The config factory.
Chris@18 48 * @param \Drupal\Core\Routing\AdminContext $admin_context
Chris@18 49 * The route admin context service.
Chris@18 50 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
Chris@18 51 * The entity repository.
Chris@0 52 */
Chris@18 53 public function __construct(EntityTypeManagerInterface $entity_type_manager, SharedTempStoreFactory $temp_store_factory, ConfigFactoryInterface $config_factory = NULL, AdminContext $admin_context = NULL, $entity_repository = NULL) {
Chris@0 54 // The config factory and admin context are new arguments due to changing
Chris@0 55 // the parent. Avoid an error on updated sites by falling back to getting
Chris@0 56 // them from the container.
Chris@0 57 // @todo Remove in 8.2.x in https://www.drupal.org/node/2674328.
Chris@0 58 if (!$config_factory) {
Chris@0 59 $config_factory = \Drupal::configFactory();
Chris@0 60 }
Chris@0 61 if (!$admin_context) {
Chris@0 62 $admin_context = \Drupal::service('router.admin_context');
Chris@0 63 }
Chris@18 64 parent::__construct($entity_type_manager, $config_factory, $admin_context, $entity_repository);
Chris@0 65
Chris@0 66 $this->tempStoreFactory = $temp_store_factory;
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * {@inheritdoc}
Chris@0 71 */
Chris@0 72 public function convert($value, $definition, $name, array $defaults) {
Chris@0 73 if (!$entity = parent::convert($value, $definition, $name, $defaults)) {
Chris@0 74 return;
Chris@0 75 }
Chris@0 76
Chris@0 77 // Get the temp store for this variable if it needs one. Attempt to load the
Chris@0 78 // view from the temp store, synchronize its status with the existing view,
Chris@0 79 // and store the lock metadata.
Chris@0 80 $store = $this->tempStoreFactory->get('views');
Chris@0 81 if ($view = $store->get($value)) {
Chris@0 82 if ($entity->status()) {
Chris@0 83 $view->enable();
Chris@0 84 }
Chris@0 85 else {
Chris@0 86 $view->disable();
Chris@0 87 }
Chris@18 88 $view->setLock($store->getMetadata($value));
Chris@0 89 }
Chris@0 90 // Otherwise, decorate the existing view for use in the UI.
Chris@0 91 else {
Chris@0 92 $view = new ViewUI($entity);
Chris@0 93 }
Chris@0 94
Chris@0 95 return $view;
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * {@inheritdoc}
Chris@0 100 */
Chris@0 101 public function applies($definition, $name, Route $route) {
Chris@0 102 if (parent::applies($definition, $name, $route)) {
Chris@0 103 return !empty($definition['tempstore']) && $definition['type'] === 'entity:view';
Chris@0 104 }
Chris@0 105 return FALSE;
Chris@0 106 }
Chris@0 107
Chris@0 108 }