You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Use an entry event such as EVENT_BEFORE_SAVE
Use a condition with $entry->getSection()->handle to check for the handle
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
The text was updated successfully, but these errors were encountered:
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.
Steps to reproduce
$entry->getSection()->handle
to check for the handleCraft 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
The text was updated successfully, but these errors were encountered: