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@0
|
6 use Drupal\Core\Entity\EntityManagerInterface;
|
Chris@0
|
7 use Drupal\Core\ParamConverter\AdminPathConfigEntityConverter;
|
Chris@0
|
8 use Drupal\Core\Routing\AdminContext;
|
Chris@0
|
9 use Symfony\Component\Routing\Route;
|
Chris@0
|
10 use Drupal\Core\ParamConverter\ParamConverterInterface;
|
Chris@0
|
11 use Drupal\user\SharedTempStoreFactory;
|
Chris@0
|
12 use Drupal\views_ui\ViewUI;
|
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@0
|
35 * @var \Drupal\user\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@0
|
42 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
Chris@0
|
43 * The entity manager.
|
Chris@0
|
44 * @param \Drupal\user\SharedTempStoreFactory $temp_store_factory
|
Chris@0
|
45 * The factory for the temp store object.
|
Chris@0
|
46 */
|
Chris@0
|
47 public function __construct(EntityManagerInterface $entity_manager, SharedTempStoreFactory $temp_store_factory, ConfigFactoryInterface $config_factory = NULL, AdminContext $admin_context = NULL) {
|
Chris@0
|
48 // The config factory and admin context are new arguments due to changing
|
Chris@0
|
49 // the parent. Avoid an error on updated sites by falling back to getting
|
Chris@0
|
50 // them from the container.
|
Chris@0
|
51 // @todo Remove in 8.2.x in https://www.drupal.org/node/2674328.
|
Chris@0
|
52 if (!$config_factory) {
|
Chris@0
|
53 $config_factory = \Drupal::configFactory();
|
Chris@0
|
54 }
|
Chris@0
|
55 if (!$admin_context) {
|
Chris@0
|
56 $admin_context = \Drupal::service('router.admin_context');
|
Chris@0
|
57 }
|
Chris@0
|
58 parent::__construct($entity_manager, $config_factory, $admin_context);
|
Chris@0
|
59
|
Chris@0
|
60 $this->tempStoreFactory = $temp_store_factory;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * {@inheritdoc}
|
Chris@0
|
65 */
|
Chris@0
|
66 public function convert($value, $definition, $name, array $defaults) {
|
Chris@0
|
67 if (!$entity = parent::convert($value, $definition, $name, $defaults)) {
|
Chris@0
|
68 return;
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 // Get the temp store for this variable if it needs one. Attempt to load the
|
Chris@0
|
72 // view from the temp store, synchronize its status with the existing view,
|
Chris@0
|
73 // and store the lock metadata.
|
Chris@0
|
74 $store = $this->tempStoreFactory->get('views');
|
Chris@0
|
75 if ($view = $store->get($value)) {
|
Chris@0
|
76 if ($entity->status()) {
|
Chris@0
|
77 $view->enable();
|
Chris@0
|
78 }
|
Chris@0
|
79 else {
|
Chris@0
|
80 $view->disable();
|
Chris@0
|
81 }
|
Chris@0
|
82 $view->lock = $store->getMetadata($value);
|
Chris@0
|
83 }
|
Chris@0
|
84 // Otherwise, decorate the existing view for use in the UI.
|
Chris@0
|
85 else {
|
Chris@0
|
86 $view = new ViewUI($entity);
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 return $view;
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * {@inheritdoc}
|
Chris@0
|
94 */
|
Chris@0
|
95 public function applies($definition, $name, Route $route) {
|
Chris@0
|
96 if (parent::applies($definition, $name, $route)) {
|
Chris@0
|
97 return !empty($definition['tempstore']) && $definition['type'] === 'entity:view';
|
Chris@0
|
98 }
|
Chris@0
|
99 return FALSE;
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 }
|