1<?php
2
3namespace Drupal\Core\Menu;
4
5use Drupal\Component\Plugin\Exception\PluginException;
6use Drupal\Core\Cache\Cache;
7use Drupal\Core\Plugin\PluginBase;
8use Drupal\Core\Url;
9
10/**
11 * Defines a base menu link class.
12 */
13abstract class MenuLinkBase extends PluginBase implements MenuLinkInterface {
14
15  /**
16   * The list of definition values where an override is allowed.
17   *
18   * The keys are definition names. The values are ignored.
19   *
20   * @var array
21   */
22  protected $overrideAllowed = [];
23
24  /**
25   * {@inheritdoc}
26   */
27  public function getWeight() {
28    // By default the weight is 0.
29    if (!isset($this->pluginDefinition['weight'])) {
30      $this->pluginDefinition['weight'] = 0;
31    }
32    return $this->pluginDefinition['weight'];
33  }
34
35  /**
36   * {@inheritdoc}
37   */
38  public function getMenuName() {
39    return $this->pluginDefinition['menu_name'];
40  }
41
42  /**
43   * {@inheritdoc}
44   */
45  public function getProvider() {
46    return $this->pluginDefinition['provider'];
47  }
48
49  /**
50   * {@inheritdoc}
51   */
52  public function getParent() {
53    return $this->pluginDefinition['parent'];
54  }
55
56  /**
57   * {@inheritdoc}
58   */
59  public function isEnabled() {
60    return (bool) $this->pluginDefinition['enabled'];
61  }
62
63  /**
64   * {@inheritdoc}
65   */
66  public function isExpanded() {
67    return (bool) $this->pluginDefinition['expanded'];
68  }
69
70  /**
71   * {@inheritdoc}
72   */
73  public function isResettable() {
74    return FALSE;
75  }
76
77  /**
78   * {@inheritdoc}
79   */
80  public function isTranslatable() {
81    return (bool) $this->getTranslateRoute();
82  }
83
84  /**
85   * {@inheritdoc}
86   */
87  public function isDeletable() {
88    return (bool) $this->getDeleteRoute();
89  }
90
91  /**
92   * {@inheritdoc}
93   */
94  public function getOptions() {
95    return $this->pluginDefinition['options'] ?: [];
96  }
97
98  /**
99   * {@inheritdoc}
100   */
101  public function getMetaData() {
102    return $this->pluginDefinition['metadata'] ?: [];
103  }
104
105  /**
106   * {@inheritdoc}
107   */
108  public function getRouteName() {
109    return isset($this->pluginDefinition['route_name']) ? $this->pluginDefinition['route_name'] : '';
110  }
111
112  /**
113   * {@inheritdoc}
114   */
115  public function getRouteParameters() {
116    return isset($this->pluginDefinition['route_parameters']) ? $this->pluginDefinition['route_parameters'] : [];
117  }
118
119  /**
120   * {@inheritdoc}
121   */
122  public function getUrlObject($title_attribute = TRUE) {
123    $options = $this->getOptions();
124    if ($title_attribute && $description = $this->getDescription()) {
125      $options['attributes']['title'] = $description;
126    }
127    if (empty($this->pluginDefinition['url'])) {
128      return new Url($this->getRouteName(), $this->getRouteParameters(), $options);
129    }
130    else {
131      return Url::fromUri($this->pluginDefinition['url'], $options);
132    }
133  }
134
135  /**
136   * {@inheritdoc}
137   */
138  public function getFormClass() {
139    return $this->pluginDefinition['form_class'];
140  }
141
142  /**
143   * {@inheritdoc}
144   */
145  public function getDeleteRoute() {
146    return NULL;
147  }
148
149  /**
150   * {@inheritdoc}
151   */
152  public function getEditRoute() {
153    return NULL;
154  }
155
156  /**
157   * {@inheritdoc}
158   */
159  public function getTranslateRoute() {
160    return NULL;
161  }
162
163  /**
164   * {@inheritdoc}
165   */
166  public function deleteLink() {
167    throw new PluginException("Menu link plugin with ID '{$this->getPluginId()}' does not support deletion");
168  }
169
170  /**
171   * {@inheritdoc}
172   */
173  public function getCacheMaxAge() {
174    return Cache::PERMANENT;
175  }
176
177  /**
178   * {@inheritdoc}
179   */
180  public function getCacheContexts() {
181    return [];
182  }
183
184  /**
185   * {@inheritdoc}
186   */
187  public function getCacheTags() {
188    return [];
189  }
190
191}
192