PHP8.1 compatibility: Warning: Trying to access array offset on value of type null in menu_form_node_form_alter()

Created on 21 August 2023, over 1 year ago
Updated 11 March 2024, 10 months ago

Problem/Motivation

When editing a node on a localized version I faced the next warnings (php8.1):
Warning: Trying to access array offset on value of type null in menu_form_node_form_alter() (line 691 of docroot/modules/menu/menu.module).
Same warnings for 702, 714, 728, 735, 746, 778 lines.
All these warnings (through they appear in the core menu module) are caused by setting $node->menu as NULL in the entity_translation_i18n_menu_node_menu_item_translate() in 295-298 lines

    // Replace the menu item with the translated version, or null if there is
    // no translated item. Store the original one in $node->source_menu.
    $node->source_menu = $node->menu;
    $node->menu = $menu;

On PHP7.4 in was a notice.

Proposed resolution

I suggest to save the $node->menu array structure:

    $node->source_menu = $node->menu;
    foreach ($node->menu as $key => $item) {
      $node->menu[$key] = $menu[$key] ?? NULL;
    }
🐛 Bug report
Status

RTBC

Version

1.0

Component

Menu integration

Created by

Live updates comments and jobs are added and updated live.
  • PHP 8.1

    The issue particularly affects sites running on PHP version 8.1.0 or later.

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024