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