Menu.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Drupal\system\Entity;
  3. use Drupal\Core\Config\Entity\ConfigEntityBase;
  4. use Drupal\Core\Entity\EntityStorageInterface;
  5. use Drupal\system\MenuInterface;
  6. /**
  7. * Defines the Menu configuration entity class.
  8. *
  9. * @ConfigEntityType(
  10. * id = "menu",
  11. * label = @Translation("Menu"),
  12. * label_collection = @Translation("Menus"),
  13. * label_singular = @Translation("menu"),
  14. * label_plural = @Translation("menus"),
  15. * label_count = @PluralTranslation(
  16. * singular = "@count menu",
  17. * plural = "@count menus",
  18. * ),
  19. * handlers = {
  20. * "access" = "Drupal\system\MenuAccessControlHandler"
  21. * },
  22. * admin_permission = "administer menu",
  23. * entity_keys = {
  24. * "id" = "id",
  25. * "label" = "label"
  26. * },
  27. * config_export = {
  28. * "id",
  29. * "label",
  30. * "description",
  31. * "locked",
  32. * }
  33. * )
  34. */
  35. class Menu extends ConfigEntityBase implements MenuInterface {
  36. /**
  37. * The menu machine name.
  38. *
  39. * @var string
  40. */
  41. protected $id;
  42. /**
  43. * The human-readable name of the menu entity.
  44. *
  45. * @var string
  46. */
  47. protected $label;
  48. /**
  49. * The menu description.
  50. *
  51. * @var string
  52. */
  53. protected $description;
  54. /**
  55. * The locked status of this menu.
  56. *
  57. * @var bool
  58. */
  59. protected $locked = FALSE;
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getDescription() {
  64. return $this->description;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function isLocked() {
  70. return (bool) $this->locked;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public static function preDelete(EntityStorageInterface $storage, array $entities) {
  76. parent::preDelete($storage, $entities);
  77. /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
  78. $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  79. foreach ($entities as $menu) {
  80. // Delete all links from the menu.
  81. $menu_link_manager->deleteLinksInMenu($menu->id());
  82. }
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function save() {
  88. $return = parent::save();
  89. \Drupal::cache('menu')->invalidateAll();
  90. // Invalidate the block cache to update menu-based derivatives.
  91. if (\Drupal::moduleHandler()->moduleExists('block')) {
  92. \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
  93. }
  94. return $return;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function delete() {
  100. parent::delete();
  101. \Drupal::cache('menu')->invalidateAll();
  102. // Invalidate the block cache to update menu-based derivatives.
  103. if (\Drupal::moduleHandler()->moduleExists('block')) {
  104. \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
  105. }
  106. }
  107. }