How to get a list of modified fields in JS when editing entry #10752
-
Is it possible to get a list of modified fields when editing an entry in the CP? I'm looking to implement a warning (JS alert) on entry save if a particular field has changed? ("Are you sure you want to update this very important field?") I see |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Craft JS doesn’t keep a definitive list currently. (The use craft\base\Element;
use craft\elements\Entry;
$draft = Entry::find()
->draftId($draftId)
->provisionalDrafts(null)
->anyStatus() // Craft 3
->status(null) // Craft 4
->one();
$modifiedFields = [];
foreach ($draft->getFieldLayout()->getFields() as $field) {
$status = $field->getStatus($draft);
if ($status && $status[0] === Element::ATTR_STATUS_MODIFIED) {
$modifiedFields[] = $field;
}
} Or you could just search the DOM for fields with a modified status indicator. |
Beta Was this translation helpful? Give feedback.
Craft JS doesn’t keep a definitive list currently. (The
delta
stuff is only for keeping track of what has changed on the current page, but doesn’t factor in any preexisting changes on the draft.) The fields themselves know, though. So you could fire an Ajax request that loads the draft and checks each of its fields.