diff --git a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php index b429213ee05a4425b428d684624308d21bfed484..ffdeb3595662a7c17280e3a23689e4867002686e 100644 --- a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php +++ b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php @@ -11,6 +11,7 @@ use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Menu\MenuParentFormSelectorInterface; use Drupal\Core\Path\PathValidatorInterface; +use Drupal\system\MenuInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -92,9 +93,9 @@ public function form(array $form, FormStateInterface $form_state) { $parent_id = $this->entity->getParentId() ?: $this->getRequest()->query->get('parent'); $default = $this->entity->getMenuName() . ':' . $parent_id; $id = $this->entity->isNew() ? '' : $this->entity->getPluginId(); - if ($this->entity->isNew()) { - $menu_id = $this->entity->getMenuName(); - $menu = $this->entityTypeManager->getStorage('menu')->load($menu_id); + $menu_id = $this->entity->getMenuName(); + $menu = $this->entityTypeManager->getStorage('menu')->load($menu_id); + if ($menu instanceof MenuInterface && $this->entity->isNew()) { $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $id, [ $menu_id => $menu->label(), ]); diff --git a/core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php b/core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php index 528c2bbf62cc14cbf18bfee5ddd1dc50a0d34b78..00d73a73f0ca61cd809c669f70eecd4c2184c3a3 100644 --- a/core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php +++ b/core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php @@ -459,4 +459,20 @@ public function testMenuLinkContentGetEntity(): void { $this->assertEquals($menu_link->id(), $tree_element->link->getEntity()->id()); } + /** + * Tests that the form doesn't break for links with arbitrary menu names. + */ + public function testMenuLinkContentFormInvalidParentMenu(): void { + $menu_link = MenuLinkContent::create([ + 'title' => 'Menu link test', + 'provider' => 'menu_link_content', + 'menu_name' => 'non-existent', + 'link' => ['uri' => 'internal:/user/login'], + ]); + // Get the form for a new link, assert that building it doesn't break if + // the links menu name doesn't exist. + $build = \Drupal::service('entity.form_builder')->getForm($menu_link); + static::assertIsArray($build); + } + }