Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Menu;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
Chris@0
|
6 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Provides a default implementation for menu link plugins.
|
Chris@0
|
10 */
|
Chris@0
|
11 class MenuLinkDefault extends MenuLinkBase implements ContainerFactoryPluginInterface {
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * {@inheritdoc}
|
Chris@0
|
15 */
|
Chris@0
|
16 protected $overrideAllowed = [
|
Chris@0
|
17 'menu_name' => 1,
|
Chris@0
|
18 'parent' => 1,
|
Chris@0
|
19 'weight' => 1,
|
Chris@0
|
20 'expanded' => 1,
|
Chris@0
|
21 'enabled' => 1,
|
Chris@0
|
22 ];
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * The static menu link service used to store updates to weight/parent etc.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var \Drupal\Core\Menu\StaticMenuLinkOverridesInterface
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $staticOverride;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Constructs a new MenuLinkDefault.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @param array $configuration
|
Chris@0
|
35 * A configuration array containing information about the plugin instance.
|
Chris@0
|
36 * @param string $plugin_id
|
Chris@0
|
37 * The plugin_id for the plugin instance.
|
Chris@0
|
38 * @param mixed $plugin_definition
|
Chris@0
|
39 * The plugin implementation definition.
|
Chris@0
|
40 * @param \Drupal\Core\Menu\StaticMenuLinkOverridesInterface $static_override
|
Chris@0
|
41 * The static override storage.
|
Chris@0
|
42 */
|
Chris@0
|
43 public function __construct(array $configuration, $plugin_id, $plugin_definition, StaticMenuLinkOverridesInterface $static_override) {
|
Chris@0
|
44 parent::__construct($configuration, $plugin_id, $plugin_definition);
|
Chris@0
|
45
|
Chris@0
|
46 $this->staticOverride = $static_override;
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * {@inheritdoc}
|
Chris@0
|
51 */
|
Chris@0
|
52 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
Chris@0
|
53 return new static(
|
Chris@0
|
54 $configuration,
|
Chris@0
|
55 $plugin_id,
|
Chris@0
|
56 $plugin_definition,
|
Chris@0
|
57 $container->get('menu_link.static.overrides')
|
Chris@0
|
58 );
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * {@inheritdoc}
|
Chris@0
|
63 */
|
Chris@0
|
64 public function getTitle() {
|
Chris@0
|
65 return (string) $this->pluginDefinition['title'];
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * {@inheritdoc}
|
Chris@0
|
70 */
|
Chris@0
|
71 public function getDescription() {
|
Chris@0
|
72 return (string) $this->pluginDefinition['description'];
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * {@inheritdoc}
|
Chris@0
|
77 */
|
Chris@0
|
78 public function isResettable() {
|
Chris@0
|
79 // The link can be reset if it has an override.
|
Chris@0
|
80 return (bool) $this->staticOverride->loadOverride($this->getPluginId());
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * {@inheritdoc}
|
Chris@0
|
85 */
|
Chris@0
|
86 public function updateLink(array $new_definition_values, $persist) {
|
Chris@0
|
87 // Filter the list of updates to only those that are allowed.
|
Chris@0
|
88 $overrides = array_intersect_key($new_definition_values, $this->overrideAllowed);
|
Chris@0
|
89 // Update the definition.
|
Chris@0
|
90 $this->pluginDefinition = $overrides + $this->getPluginDefinition();
|
Chris@0
|
91 if ($persist) {
|
Chris@0
|
92 // Always save the menu name as an override to avoid defaulting to tools.
|
Chris@0
|
93 $overrides['menu_name'] = $this->pluginDefinition['menu_name'];
|
Chris@0
|
94 $this->staticOverride->saveOverride($this->getPluginId(), $this->pluginDefinition);
|
Chris@0
|
95 }
|
Chris@0
|
96 return $this->pluginDefinition;
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 }
|