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

Issue fork og_menu-3392055

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

barone created an issue. See original summary.

barone’s picture

Status: Active » Needs review
StatusFileSize
new459 bytes
olivierg’s picture

StatusFileSize
new808 bytes

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.

barone’s picture

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!

dimilias’s picture

StatusFileSize
new1.71 KB

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.

dimilias’s picture

StatusFileSize
new1.71 KB

Fixed a php sniff.

alorenc’s picture

I think we need to combine checks from #4 and #5, check if entity and id is not empty

claudiu.cristea’s picture

Hide old patches

claudiu.cristea’s picture

Version: 2.0.0-alpha2 » 2.x-dev
Status: Needs review » Reviewed & tested by the community

Looks good

claudiu.cristea’s picture

Assigned: barone » Unassigned
Status: Reviewed & tested by the community » Fixed

Thank you!

claudiu.cristea’s picture

Released in 2.0.0-alpha3

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.