diff --git a/CHANGELOG.md b/CHANGELOG.md index 042a384b4db..928bb079964 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Element indexes now sort by ID by default, for sources that don’t define a default sort option. - Fixed a bug where element indexes were sorting by the first sortable attribute alphabetically by default, rather than the first sortable attribute defined by the element type. - Fixed a bug where bulk asset actions where shown as available when subfolders were selected, when they shouldn’t have. ([#16151](https://github.com/craftcms/cms/issues/16151)) +- Fixed a bug where `craft\events\ApplyFieldSaveEvent::$field` wasn’t being set consistently by `craft\services\Fields::EVENT_BEFORE_APPLY_FIELD_SAVE`. ([#16156](https://github.com/craftcms/cms/issues/16156)) ## 4.13.2 - 2024-11-19 diff --git a/src/events/ApplyFieldSaveEvent.php b/src/events/ApplyFieldSaveEvent.php index 6040622d7af..fe1981cc3ef 100644 --- a/src/events/ApplyFieldSaveEvent.php +++ b/src/events/ApplyFieldSaveEvent.php @@ -19,8 +19,8 @@ class ApplyFieldSaveEvent extends Event { /** - * @var FieldInterface|null The field associated with this event, if it already exists - * in the database or in memory. + * @var FieldInterface|null The field associated with this event, as + * configured before the changes are applied to it (if it already exists). */ public ?FieldInterface $field; diff --git a/src/services/Fields.php b/src/services/Fields.php index 55288473999..ebd50cfb2f0 100644 --- a/src/services/Fields.php +++ b/src/services/Fields.php @@ -1571,20 +1571,15 @@ public function applyFieldSave(string $fieldUid, array $data, string $context): $groupRecord = $groupUid ? $this->_getGroupRecord($groupUid) : null; $isNewField = $fieldRecord->getIsNewRecord(); $oldSettings = $fieldRecord->getOldAttribute('settings'); + $oldField = !$isNewField ? $this->getFieldById($fieldRecord->id) : null; // For control panel save requests, make sure we have all the custom data already saved on the object. - if (isset($this->_savingFields[$fieldUid])) { - $field = $this->_savingFields[$fieldUid]; - } elseif (!$isNewField) { - $field = $this->getFieldById($fieldRecord->id); - } else { - $field = null; - } + $field = $this->_savingFields[$fieldUid] ?? $oldField; // Fire a 'beforeApplyFieldSave' event if ($this->hasEventHandlers(self::EVENT_BEFORE_APPLY_FIELD_SAVE)) { $this->trigger(self::EVENT_BEFORE_APPLY_FIELD_SAVE, new ApplyFieldSaveEvent([ - 'field' => $field, + 'field' => $oldField, 'config' => $data, ])); }