From 84432de226d6477dc02164945f7d9192eaaf0d19 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Sat, 30 Mar 2024 17:03:59 +0000 Subject: [PATCH] Issue #3411384 by phthlaap, venkatadapa, alecsmrekar, plach, alexpott, heddn, Graber: Error: Call to a member function label() on null in Drupal\menu_link_content\Form\MenuLinkContentForm->form() (line 99 of /var/www/html/docroot/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php) --- .../src/Form/MenuLinkContentForm.php | 7 ++++--- .../tests/src/Kernel/MenuLinksTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php index b429213ee05a..ffdeb3595662 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 ff9cc7f33691..b39df7f9aeae 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); + } + } -- GitLab