Build method throws Undefined array key -1 when block is on top-level page

Created on 25 February 2022, over 2 years ago
Updated 30 November 2023, 12 months ago

Problem/Motivation

Frequently seeing the error
Warning: Undefined array key -1 in Drupal\menu_block\Plugin\Block\MenuBlock->build() (line 280 of /var/www/html/web/modules/contrib/menu_block/src/Plugin/Block/MenuBlock.php)

coming from top-level pages containing this block.

Steps to reproduce

Place the block on a page with no parent.

Proposed resolution

The problem seems to be that the $menu_trail_ids array contains only 2 items in this case, so subtracting 3 from the count yields -1.

Calculating this value first then testing for less than 0 and adjusting up to 0 in that case solves the problem.

🐛 Bug report
Status

Closed: cannot reproduce

Version

1.7

Component

Code

Created by

🇨🇦Canada jodavidson

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024