diff --git a/core/modules/menu_link_content/menu_link_content.install b/core/modules/menu_link_content/menu_link_content.install index 6086ac08f5..d59cfc484d 100644 --- a/core/modules/menu_link_content/menu_link_content.install +++ b/core/modules/menu_link_content/menu_link_content.install @@ -5,9 +5,23 @@ * Install, update and uninstall functions for the menu_link_content module. */ +use Drupal\Core\Field\BaseFieldDefinition; + /** * Implements hook_update_last_removed(). */ function menu_link_content_update_last_removed() { return 8601; } + +/** + * Implements hook_update_N(). + */ +function menu_link_content_update_8602() { + $new_menu_storage_definition = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Menu')) + ->setDescription(t('The menu this link is part of.')); + $entityUpdateManager = \Drupal::entityDefinitionUpdateManager(); + + $entityUpdateManager->installFieldStorageDefinition('menu', 'menu_link_content', 'menu_link_content', $new_menu_storage_definition); +} \ No newline at end of file diff --git a/core/modules/menu_link_content/src/Entity/MenuLinkContent.php b/core/modules/menu_link_content/src/Entity/MenuLinkContent.php index 90ff62eaea..3c48a79bb5 100644 --- a/core/modules/menu_link_content/src/Entity/MenuLinkContent.php +++ b/core/modules/menu_link_content/src/Entity/MenuLinkContent.php @@ -8,6 +8,7 @@ use Drupal\Core\Field\BaseFieldDefinition; use Drupal\link\LinkItemInterface; use Drupal\menu_link_content\MenuLinkContentInterface; +use Drupal\system\MenuInterface; /** * Defines the menu link content entity class. @@ -83,6 +84,14 @@ public function setInsidePlugin() { /** * {@inheritdoc} */ + public function setMenu(MenuInterface $menu) { + $this->set('menu', $menu->id()); + return $this; + } + + /** + * {@inheritdoc} + */ public function getTitle() { return $this->get('title')->value; } @@ -104,6 +113,13 @@ public function getMenuName() { /** * {@inheritdoc} */ + public function getMenu() { + return $this->get('menu')->entity; + } + + /** + * {@inheritdoc} + */ public function getDescription() { return $this->get('description')->value; } @@ -168,6 +184,7 @@ public function getPluginDefinition() { } $definition['title'] = $this->getTitle(); + $definition['menu'] = $this->getMenu(); $definition['description'] = $this->getDescription(); $definition['weight'] = $this->getWeight(); $definition['id'] = $this->getPluginId(); @@ -323,6 +340,10 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setDefaultValue('tools') ->setSetting('is_ascii', TRUE); + $fields['menu'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Menu')) + ->setDescription(t('The menu this link is part of.')); + $fields['link'] = BaseFieldDefinition::create('link') ->setLabel(t('Link')) ->setDescription(t('The location this menu link points to.')) diff --git a/core/modules/menu_link_content/src/MenuLinkContentInterface.php b/core/modules/menu_link_content/src/MenuLinkContentInterface.php index 583d162097..5acdb518d0 100644 --- a/core/modules/menu_link_content/src/MenuLinkContentInterface.php +++ b/core/modules/menu_link_content/src/MenuLinkContentInterface.php @@ -5,6 +5,7 @@ use Drupal\Core\Entity\EntityChangedInterface; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityPublishedInterface; +use Drupal\system\MenuInterface; use Drupal\Core\Entity\RevisionLogInterface; /** @@ -18,6 +19,16 @@ public function setInsidePlugin(); /** + * Sets the menu entity of the custom menu link. + * + * @param \Drupal\system\MenuInterface $menu + * The menu entity. + * + * @return $this + */ + public function setMenu(MenuInterface $menu); + + /** * Gets the title of the menu link. * * @return string @@ -42,6 +53,14 @@ public function getUrlObject(); public function getMenuName(); /** + * Gets the menu entity of the custom menu link. + * + * @return \Drupal\system\MenuInterface + * The menu entity. + */ + public function getMenu(); + + /** * Gets the description of the menu link for the UI. * * @return string