Undefined array key warning when only 1 child is present within a menu item

Created on 16 February 2023, over 1 year ago
Updated 5 May 2023, about 1 year ago

Problem/Motivation

When having the "Restrict to parent" setting enabled, if you visit a node that belongs to a parent menu item and it is the ONLY child, you'll see a Drupal warning message about "Undefined array key".

Example warning message:

Warning: Undefined array key "main" in Drupal\menu_pager\Plugin\Block\MenuBlock->menuPagerGetNavigation() (line 283 of modules/contrib/menu_pager/src/Plugin/Block/MenuBlock.php).
Drupal\menu_pager\Plugin\Block\MenuBlock->menuPagerGetNavigation(Object, 1) (Line: 184)
Drupal\menu_pager\Plugin\Block\MenuBlock->build() (Line: 171)
Drupal\block\BlockViewBuilder::preRender(Array)
call_user_func_array(Array, Array) (Line: 101)
Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 788)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 374)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 204)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 160)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 580)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 161)
Drupal\Core\Render\Renderer->renderPlain(Array) (Line: 175)
Drupal\Core\Render\Renderer->renderPlaceholder('callback=Drupal%5Cblock%5CBlockViewBuilder%3A%3AlazyBuilder&args%5B0%5D=menupagermainnavigation&args%5B1%5D=full&args%5B2%5D&token=0_cyHwwBOFUTBFeK5CjtVkePhMbaiiSE9rUsT40opBg', Array) (Line: 693)
Drupal\big_pipe\Render\BigPipe->renderPlaceholder('callback=Drupal%5Cblock%5CBlockViewBuilder%3A%3AlazyBuilder&args%5B0%5D=menupagermainnavigation&args%5B1%5D=full&args%5B2%5D&token=0_cyHwwBOFUTBFeK5CjtVkePhMbaiiSE9rUsT40opBg', Array) (Line: 547)
Drupal\big_pipe\Render\BigPipe->sendPlaceholders(Array, Array, Object) (Line: 305)
Drupal\big_pipe\Render\BigPipe->sendContent(Object) (Line: 112)
Drupal\big_pipe\Render\BigPipeResponse->sendContent() (Line: 381)
Symfony\Component\HttpFoundation\Response->send() (Line: 20)

Steps to reproduce

  1. Place the menu pager block within a region. Make sure "Restrict to parent" is enabled for the block.
  2. Add a node to a menu item and then another node as a child to that menu item.
  3. Visit the node page from the child menu item and you'll see the "Undefined array key" warning.
  4. If you add another child menu item so there's more than just 1 child, the warning will be gone.

Example menu structure to re-produce the issue:

  • Sample Menu Item
    • Sample Sub-Menu Item

Proposed resolution

Check the array before trying to return the pager navigation.

πŸ› Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada cbildstein

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

Comments & Activities

Production build 0.69.0 2024