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