Pull merge.
[yaffs-website] / web / core / modules / views / src / Plugin / Menu / ViewsMenuLink.php
1 <?php
2
3 namespace Drupal\views\Plugin\Menu;
4
5 use Drupal\Core\Menu\MenuLinkBase;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\views\ViewExecutableFactory;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Defines menu links provided by views.
13  *
14  * @see \Drupal\views\Plugin\Derivative\ViewsMenuLink
15  */
16 class ViewsMenuLink extends MenuLinkBase implements ContainerFactoryPluginInterface {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected $overrideAllowed = [
22     'menu_name' => 1,
23     'parent' => 1,
24     'weight' => 1,
25     'expanded' => 1,
26     'enabled' => 1,
27     'title' => 1,
28     'description' => 1,
29   ];
30
31   /**
32    * The entity manager.
33    *
34    * @var \Drupal\Core\Entity\EntityManagerInterface
35    */
36   protected $entityManager;
37
38   /**
39    * The view executable factory.
40    *
41    * @var \Drupal\views\ViewExecutableFactory
42    */
43   protected $viewExecutableFactory;
44
45   /**
46    * The view executable of the menu link.
47    *
48    * @var \Drupal\views\ViewExecutable
49    */
50   protected $view;
51
52   /**
53    * Constructs a new ViewsMenuLink.
54    *
55    * @param array $configuration
56    *   A configuration array containing information about the plugin instance.
57    * @param string $plugin_id
58    *   The plugin_id for the plugin instance.
59    * @param mixed $plugin_definition
60    *   The plugin implementation definition.
61    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
62    *   The entity manager
63    * @param \Drupal\views\ViewExecutableFactory $view_executable_factory
64    *   The view executable factory
65    */
66   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, ViewExecutableFactory $view_executable_factory) {
67     parent::__construct($configuration, $plugin_id, $plugin_definition);
68
69     $this->entityManager = $entity_manager;
70     $this->viewExecutableFactory = $view_executable_factory;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
77     return new static(
78       $configuration,
79       $plugin_id,
80       $plugin_definition,
81       $container->get('entity.manager'),
82       $container->get('views.executable')
83     );
84   }
85
86   /**
87    * Initializes the proper view.
88    *
89    * @return \Drupal\views\ViewExecutable
90    *   The view executable.
91    */
92   public function loadView() {
93     if (empty($this->view)) {
94       $metadata = $this->getMetaData();
95       $view_id = $metadata['view_id'];
96       $display_id = $metadata['display_id'];
97       $view_entity = $this->entityManager->getStorage('view')->load($view_id);
98       $view = $this->viewExecutableFactory->get($view_entity);
99       $view->setDisplay($display_id);
100       $view->initDisplay();
101       $this->view = $view;
102     }
103     return $this->view;
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function getTitle() {
110     // @todo Get the translated value from the config without instantiating the
111     //   view. https://www.drupal.org/node/2310379
112     return $this->loadView()->display_handler->getOption('menu')['title'];
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function getDescription() {
119     return $this->loadView()->display_handler->getOption('menu')['description'];
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   public function isExpanded() {
126     return (bool) $this->loadView()->display_handler->getOption('menu')['expanded'];
127   }
128
129   /**
130    * {@inheritdoc}
131    */
132   public function updateLink(array $new_definition_values, $persist) {
133     $overrides = array_intersect_key($new_definition_values, $this->overrideAllowed);
134     // Update the definition.
135     $this->pluginDefinition = $overrides + $this->pluginDefinition;
136     if ($persist) {
137       $view = $this->loadView();
138       $display = &$view->storage->getDisplay($view->current_display);
139       // Just save the title to the original view.
140       $changed = FALSE;
141       foreach ($overrides as $key => $new_definition_value) {
142         if (empty($display['display_options']['menu'][$key]) || $display['display_options']['menu'][$key] != $new_definition_value) {
143           $display['display_options']['menu'][$key] = $new_definition_value;
144           $changed = TRUE;
145         }
146       }
147       if ($changed) {
148         // @todo Improve this to not trigger a full rebuild of everything, if we
149         //   just changed some properties. https://www.drupal.org/node/2310389
150         $view->storage->save();
151       }
152     }
153     return $this->pluginDefinition;
154   }
155
156   /**
157    * {@inheritdoc}
158    */
159   public function isDeletable() {
160     return TRUE;
161   }
162
163   /**
164    * {@inheritdoc}
165    */
166   public function deleteLink() {
167   }
168
169 }