Can't create group menu link after upgrade to 10.2.2 - Error: Call to a member function label() on null

Created on 29 January 2024, 5 months ago
Updated 2 April 2024, 3 months ago

Problem/Motivation

I had this module working OK on a build, but today noticed I can no longer add group menu links. I get this error...

Error: Call to a member function label() on null in Drupal\menu_link_content\Form\MenuLinkContentForm->form() (line 99 of core/modules/menu_link_content/src/Form/MenuLinkContentForm.php).

See attached for full error.

This issue has been reported as a core issue here: https://www.drupal.org/project/drupal/issues/3411384 🐛 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 and the patch on comment 2 fixes the problem, but I notice a core contributor asked if this was the right place to fix this, so I thought I would capture this here in the Group Content Menu issue queue.

Steps to reproduce

  1. Upgrade to Drupal 10.2.2
  2. Create a Group
  3. Create a menu for the group
  4. Attempt to add a link
  5. Get fatal error
🐛 Bug report
Status

Closed: duplicate

Version

3.0

Component

User interface

Created by

🇺🇸United States banoodle San Francisco, CA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • 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 3 months ago
  • 🇺🇸United States scotwith1t Birmingham, AL
Production build 0.69.0 2024