- Issue created by @banoodle
- 🇮🇳India sumit-k
Upon analysis of the addLink() function in the
modules/contrib/group_content_menu/src/Controller/GroupContentMenuController.php
file, it appears that there is an issue with loading the menu entity, resulting in a null value for the menu. This occurs because the entity type and menu ID are not correctly specified.public function addLink(GroupContentMenuInterface $group_content_menu) { $menu_name = GroupContentMenuInterface::MENU_PREFIX . $group_content_menu->id(); $menu_link = $this->entityTypeManager()->getStorage('menu_link_content')->create([ 'menu_name' => $menu_name, 'bundle' => 'menu_link_content', ]); return $this->entityFormBuilder()->getForm($menu_link); }
addLink function is calling
/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php
public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $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); $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $id, [ $menu_id => $menu->label(), ]); } else { $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $id); } $form['menu_parent']['#weight'] = 10; $form['menu_parent']['#title'] = $this->t('Parent link'); $form['menu_parent']['#description'] = $this->t('The maximum depth for a link and all its children is fixed. Some menu links may not be available as parents if selecting them would exceed this limit.'); $form['menu_parent']['#attributes']['class'][] = 'menu-title-select'; return $form; }
$menu = $this->entityTypeManager->getStorage('menu')->load($menu_id);
menu is not getting loaded it is coming null because entity type and menu id are not correct.
To address this issue, attempted to correct the entity type and ID by directly setting them to 'group_content_menu' and a valid menu ID.
something like$menu_id = '9'; // Check menu id in my case it is 9 $menu = $this->entityTypeManager->getStorage('group_content_menu')->load($menu_id);
IMO Instead of modifying the core files, it may be more appropriate to create a custom form function within the Group Content Menu module. By doing so, you can ensure that the correct entity type and menu ID are used without altering core functionality.
In case of group content menu the menu id isn't an integer but is a string like group_menu_link_content-9.
$this->entityTypeManager->getStorage('group_content_menu')->load($menu_id);
returns null- 🇺🇸United States scotwith1t Birmingham, AL
Duplicate of 🐛 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). Fixed . Fix is in D10.3-dev, 10.2-dev, so should be in 10.2.5 (and, of course, 10.3 when stable comes out). https://git.drupalcode.org/project/drupal/-/merge_requests/7228.diff is the patch file if you want to just add to composer.json.
- Status changed to Closed: duplicate
11 months ago 9:28pm 2 April 2024