Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x]: Attempt to read property "handle" on null for entry based events #16114

Open
jamesmacwhite opened this issue Nov 14, 2024 · 0 comments
Open
Labels

Comments

@jamesmacwhite
Copy link
Contributor

jamesmacwhite commented Nov 14, 2024

What happened?

Description

We have various events which are using entry events like EVENT_BEFORE_SAVE, EVENT_AFTER_SAVE or EVENT_DEFINE_SIDEBAR_HTML with instanceof Entry to detect entry saves/entries.

Various events are using $entry->getSection()->handle from the $event->sender; to determine if it is the right entry section to do further logic. However on several entry saves these events appear to pick up saves on entry data where the section handle property is null and doesn't exist. Samples of this are matrix content, other element types etc.

It is a while since I've looked into these events, but under Craft 3, checking the handle seems generally reliable, however under Craft 5, this might not be the case now due to changes.

This might not be a bug, but just looking for further guidance on the safest way to handle this, given the behaviour differences. Essentially the events are intending to target a specific section which is Entries, I can simply do this, to be defensive if that is suited.

if ($entry->getSection() && $entry->getSection()->handle === 'handle') {
  // Do things when the section handle required
}

Steps to reproduce

  1. Use an entry event such as EVENT_BEFORE_SAVE
  2. Use a condition with $entry->getSection()->handle to check for the handle
  3. In cases where the handle property is null, this causes an error. Observed on saves of content like matrix.

Craft CMS version

5.5.0.1

PHP version

8.3

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant