-
Notifications
You must be signed in to change notification settings - Fork 641
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
[4.x]: getDirtyFields always returns all fields in a hook #12967
Comments
How is the entry being saved in the first place? |
@brandonkelly Just normally from the Control Panel: all-fields-are-dirty.mov |
Technically this is working as expected: dirty fields will only be accurate on POST requests from the control panel, and only for the element that the post data is getting applied to. In most cases, that element will be the provisional draft that automatically gets created the first time the entry gets autosaved, not the canonical element. (The only exceptions are if To capture the fields that were modified on a draft, you’ll need to add this code as well: use craft\events\DraftEvent;
use craft\services\Drafts;
use yii\base\Event;
Event::on(
Drafts::class,
Drafts::EVENT_BEFORE_APPLY_DRAFT,
function(DraftEvent $event) {
$modifiedFieldHandles = $event->draft->getModifiedFields();
// ...
}
); All that said, I can see how it would be expected that |
@brandonkelly Thanks for the thorough explanation! And thanks for the adjustment, I think that makes sense.
Just so I understand completely: If this is the case, why was
Thanks, that's looking good! |
If the specific dirty fields isn’t known, the element will just report all fields as dirty, to err on the side of caution.
We are admittedly not really using interfaces correctly. We still expect that all elements extend (Eventually we will probably phase the interfaces out completely in favor of the base classes, since there’s no practical way to provide a custom element type/field type by implementing the interface directly.) |
@brandonkelly Thanks again, I understand now!
Off-topic now, but I think the root problem is that the But even if all element types need to extend the |
We do a bit of that – with
Well you’d be able to use |
@brandonkelly Absolutely agree! Structures are a good example, since most element types aren't structures, so having a separate interface just for that would make sense.
@brandonkelly Yeah, but the class may have public methods that are not part of the |
Craft 4.5.0 is out with that change to |
Sorry to bother since the issue is closed, but I wonder if my scenario is included in what is reported above: I'm having the same difficulties to retrieve modified/outdated custom fields in a User element (which, as far as I know, has no drafts). |
@masiorama Users don’t currently support dirty field tracking, but they will in Craft 5. |
@brandonkelly can you confirm dirty fields work on user entity? I'm about to make a huge upgrade to my platform just for this (didn't plan to upgrade to v5 for this project). Thanks. |
@masiorama In Craft 5, |
Thanks, I had the chance to edit my code after upgrading to craft5 and it does work as you described. |
What happened?
Description
I'm trying to write a hook for
Entry::EVENT_BEFORE_SAVE
where I need to get a list of fields that have changed relative to the canonical element. This is surprisingly difficult:This always returns an array with all field handles of the entry, not only those that have changed. Is this a bug or am I doing something wrong?
I've also tried some other methods:
getModifiedFields()
returns either an empty array or an array of all fields on the entry, depending on the state of the entry / draft being saved.getOutdatedFields
returns an empty array.How can I actually get a list of fields that have changed / are going to change during the pending save?
Steps to reproduce
Expected behavior
getModifiedFields()
should only return the fields that have actually been modified, not all fields.Also, the documentation (or the docblocks for the
ElementInterface
) for the three mentioned methods could be improved. They all sound very similar and to an outside observer it's difficult to understand in what scenarios they can and can't be used.Actual behavior
getModifiedFields
returns all field handles, regardless of which fields have actually changed.Craft CMS version
4.4.5
PHP version
8.2
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions
No response
The text was updated successfully, but these errors were encountered: