diff --git a/core/modules/jsonapi/tests/src/Functional/MenuLinkContentTest.php b/core/modules/jsonapi/tests/src/Functional/MenuLinkContentTest.php index 05d7f47c24..33d7da7cf2 100644 --- a/core/modules/jsonapi/tests/src/Functional/MenuLinkContentTest.php +++ b/core/modules/jsonapi/tests/src/Functional/MenuLinkContentTest.php @@ -137,6 +137,17 @@ protected function getExpectedDocument() { ], ], ], + 'menu' => [ + 'data' => NULL, + 'links' => [ + 'related' => [ + 'href' => $self_url . '/menu', + ], + 'self' => [ + 'href' => $self_url . '/relationships/menu', + ], + ], + ], ], ], ]; diff --git a/core/modules/menu_link_content/menu_link_content.install b/core/modules/menu_link_content/menu_link_content.install index 6086ac08f5..718fea0a51 100644 --- a/core/modules/menu_link_content/menu_link_content.install +++ b/core/modules/menu_link_content/menu_link_content.install @@ -5,9 +5,26 @@ * 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')) + ->setSetting('target_type', 'menu') + ->setRequired(TRUE) + ->setDefaultValue(['target_id' => 'main']) + ->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..e8526d477a 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. @@ -80,6 +81,14 @@ public function setInsidePlugin() { $this->insidePlugin = TRUE; } + /** + * {@inheritdoc} + */ + public function setMenu(MenuInterface $menu) { + $this->set('menu', $menu->id()); + return $this; + } + /** * {@inheritdoc} */ @@ -101,6 +110,13 @@ public function getMenuName() { return $this->get('menu_name')->value; } + /** + * {@inheritdoc} + */ + public function getMenu() { + return $this->get('menu')->entity; + } + /** * {@inheritdoc} */ @@ -323,6 +339,13 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setDefaultValue('tools') ->setSetting('is_ascii', TRUE); + $fields['menu'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Menu')) + ->setSetting('target_type', 'menu') + ->setRequired(TRUE) + ->setDefaultValue(['target_id' => 'main']) + ->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/Form/MenuLinkContentForm.php b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php index a7525c0ddc..3e52b68cbb 100644 --- a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php +++ b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php @@ -115,6 +115,7 @@ public function buildEntity(array $form, FormStateInterface $form_state) { $entity->parent->value = $parent; $entity->menu_name->value = $menu_name; + $entity->menu->target_id = $menu_name; $entity->enabled->value = (!$form_state->isValueEmpty(['enabled', 'value'])); $entity->expanded->value = (!$form_state->isValueEmpty(['expanded', 'value'])); 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; /** @@ -17,6 +18,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. * @@ -41,6 +52,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. * diff --git a/core/modules/menu_link_content/tests/src/Functional/Rest/MenuLinkContentResourceTestBase.php b/core/modules/menu_link_content/tests/src/Functional/Rest/MenuLinkContentResourceTestBase.php index 3cdf2eff73..a54b2237e9 100644 --- a/core/modules/menu_link_content/tests/src/Functional/Rest/MenuLinkContentResourceTestBase.php +++ b/core/modules/menu_link_content/tests/src/Functional/Rest/MenuLinkContentResourceTestBase.php @@ -149,6 +149,13 @@ protected function getExpectedNormalizedEntity() { 'value' => 'main', ], ], + 'menu' => [ + [ + 'target_id' => 'main', + 'target_type' => 'menu', + 'target_uuid' => $this->entity->menu->entity->uuid() + ], + ], 'langcode' => [ [ 'value' => 'en',