MenuLinkDefault.php 2.7 KB

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