MenuLinkBase.php 3.5 KB

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