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