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
[3.2] isNew is never set in EVENT_BEFORE_SAVE_ELEMENT
#4589
Comments
|
Relevant code referenced -> https://github.com/nystudio107/craft-retour/blob/develop/src/Retour.php#L295 |
|
So maybe replace this line: $oldElement = Craft::$app->getElements()->getElementById($element->id);with: $oldElement = $element::find()
->id($element->id)
->siteId($element->siteId)
->anyStatus()
->one();At the point when an unsaved draft is first getting saved into a real entry, if (Retour::$craft32 && ElementHelper::isDraftOrRevision($oldElement)) {
$checkElementSlug = false;
} |
Signed-off-by: Andrew Welch <andrew@nystudio107.com>
|
Here's what we ended up with, if it helps anyone searching on this: nystudio107/craft-retour@9de8d09 The reason we didn't go with the above from Brandon is that by the time the element is getting saved, the DB has already been updated to drop its https://github.com/craftcms/cms/blob/develop/src/services/Drafts.php#L243-L258 So instead we're checking the element's URI for We also decided that fetching $oldElement is a little pointless since $element will still have its old URI. |
|
It seems like removing the is unnecessary since it then sets but I could be very wrong :) |
|
In Craft 3.2, a simplification is that you have 3 states for Elements:
Imo most people expect And I think there's a good bit of code, both plugins and modules, that things won't work quite as expected because of this. Granted, there may have been logic issues already with pre-Craft 3.2, but the immediate auto-save to draft after making a new entry likely brings it to the fore. |
|
@putyourlightson If we don’t set |
|
Ah yes, thanks for the clarification @brandonkelly. |
|
We just released Craft 3.2.5, which changes the behavior of saving a new entry from the unpublished draft state. Now Craft assigns the entry a new ID at that point, so |
My plugin listens for
Elements::EVENT_BEFORE_SAVE_ELEMENTand has different behavior depending on the passed inisNewproperty of theElementEvent.Unfortunately, in Craft 3.2.3 it appears that
isNewnever evaluates totrue:I haven't traced through the code fully yet, but I'm suspecting that the reason it's never set is due to the changes in the draft mechanism, the element always has an
idas soon as the auto-save of a draft kicks in (which happens almost immediately).This results in unexpected behavior in my plugin, since it tries to do different things depending on whether the entry is new or not.
Here's an example dump of
print_r($element->getAttributes(), true)on an element as passed into myEVENT_BEFORE_SAVE_ELEMENThandler:Still inside of the
EVENT_BEFORE_SAVE_ELEMENTsinceisNewisfalseI then attempt to load the saved element from the db to compare it to what we're about to save via:Oddly, even though it's never been saved before other than as an auto-saved draft,
print_r($oldElement->getAttributes(), true)doesn't show that it's a draft or a revision or anything of the kind:As it stands, I can't see how we can distinguish an element that
isNewhere.The text was updated successfully, but these errors were encountered: