I don't know if it was with Craft 3.5, but did something change slightly when using the EVENT_BEFORE_SAVE event?
I have a simple use case where I am encoding the entry ID and updating the slug when the entry is saved. It does check if a title is empty and if the entry is a draft or revision, if any of those are true, then it does not encode it.
After updating from Craft 3.4.30 to Craft 3.5.5 when saving an entry I have noticed that it throws an error as $entry->id returns null, however on refreshing the page and then resaving it, it passes the entry ID in fine.
A basic example of what I have is:
Event::on(
Entry::class,
Entry::EVENT_BEFORE_SAVE,
function (ModelEvent $event) {
$entry = $event->sender;
if (ElementHelper::isDraftOrRevision($entry)) {
return false;
}
// Get entry title
$title = $entry->title;
if ($title !== null) {
// Encode entry ID
$newSlug = Module::$instance->hashid->encode($entry->id);
// Update slug to be a hash
$entry->slug = $newSlug;
}
}
);
Before hand this worked fine when creating the entry, but now it is requiring me to have to save the entries twice, any thoughts?
$entry->id
wouldn't have been set yet (because it hasn't been saved yet) and it doesn't look like your code is taking that into account. Are you sure that's not what you're seeing?