Error: Typed property MenuName accessed before initialization

Created on 5 October 2023, 9 months ago
Updated 5 January 2024, 6 months ago

Problem/Motivation

When saving a node associated with an OG menu instance, the OgMenuBlock Plugin throws an error at the first load since the property does not exists yet. Upon reload, the property was cached and the page render as expected.

Steps to reproduce

Setup
- Drupal 10.1.x
- PHP 8.2.3
- Organic groups 8.x-1.0-alpha10

Create a new node that's associated with an OG and add it to the Group's menu. Save the node and you should get the following error:

The website encountered an unexpected error. Please try again later.

Error: Typed property Drupal\og_menu\Plugin\Block\OgMenuBlock::$menuName must not be accessed before initialization in Drupal\og_menu\Plugin\Block\OgMenuBlock->getMenuName() (line 315 of modules/contrib/og_menu/src/Plugin/Block/OgMenuBlock.php).

Proposed resolution

initialize the $menuName property in the constructor of the OgMenuBlock class

Remaining tasks

Add code to src/Plugin/Block/OgMenuBlock.php

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

πŸ‡§πŸ‡·Brazil barone Belo Horizonte

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

Comments & Activities

  • Issue created by @barone
  • Status changed to Needs review 9 months ago
  • Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 9 months ago
    Waiting for branch to pass
  • πŸ‡§πŸ‡·Brazil barone Belo Horizonte
  • Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 8 months ago
    Waiting for branch to pass
  • πŸ‡¨πŸ‡­Switzerland OlivierG

    Hi,
    the patch does not work correctly cause the getMenuName function doesn't get the name you already defined as blank in the constructor.
    So we have to change the getMenuName function in order to check menuName is not blank.

  • Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 8 months ago
    Waiting for branch to pass
  • πŸ‡§πŸ‡·Brazil barone Belo Horizonte

    Hi @OliverG thanks for the replay and for the patch. I'm adding a conditional check to avoid getting the error of calling ->id() on NULL. If this one works for you we may get this issue closed!

  • Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 6 months ago
    Waiting for branch to pass
  • πŸ‡¬πŸ‡·Greece idimopoulos

    I am making a slight change to check for the ID, not just the entity. The reason is that OG menu seems to break the layout builder which passes as context an entity without an ID.

  • Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 6 months ago
    Waiting for branch to pass
  • πŸ‡¬πŸ‡·Greece idimopoulos

    Fixed a php sniff.

Production build 0.69.0 2024