Skip to content

Commit

Permalink
Merge branch '4.13' of https://github.com/craftcms/cms into 5.5
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG-WIP.md
  • Loading branch information
brandonkelly committed Oct 15, 2024
2 parents 7808c9e + 423aaf7 commit dcaee97
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
11 changes: 10 additions & 1 deletion src/events/ApplyFieldSaveEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@

namespace craft\events;

use craft\base\Event;
use craft\base\FieldInterface;

/**
* ApplyFieldSaveEvent class.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 5.5.0
*/
class ApplyFieldSaveEvent extends FieldEvent
class ApplyFieldSaveEvent extends Event
{
/**
* @var FieldInterface|null The field associated with this event, if it already exists
* in the database or in memory.
*/
public ?FieldInterface $field;

/**
* @var array New field config data that is about to be applied.
*/
Expand Down
24 changes: 13 additions & 11 deletions src/services/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -1345,18 +1345,10 @@ public function applyFieldSave(string $fieldUid, array $data, string $context):
// 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];

if ($isNewField) {
$field->id = $fieldRecord->id;
}
} else {
} elseif (!$isNewField) {
$field = $this->getFieldById($fieldRecord->id);
}

if (!$isNewField) {
// Save the old field handle and settings on the model in case the field type needs to do something with it.
$field->oldHandle = $fieldRecord->getOldHandle();
$field->oldSettings = is_string($oldSettings) ? Json::decode($oldSettings) : null;
} else {
$field = null;
}

// Fire a 'beforeApplyFieldSave' event
Expand Down Expand Up @@ -1414,6 +1406,16 @@ public function applyFieldSave(string $fieldUid, array $data, string $context):
// Tell the current CustomFieldBehavior class about the field
CustomFieldBehavior::$fieldHandles[$fieldRecord->handle] = true;

if ($isNewField) {
// Try fetching the field again, if it didn’t exist to begin with
$field ??= $this->getFieldById($fieldRecord->id);
$field->id = $fieldRecord->id;
} else {
// Save the old field handle and settings on the model in case the field type needs to do something with it.
$field->oldHandle = $fieldRecord->getOldHandle();
$field->oldSettings = is_string($oldSettings) ? Json::decode($oldSettings) : null;
}

$field->afterSave($isNewField);

// Fire an 'afterSaveField' event
Expand Down

0 comments on commit dcaee97

Please sign in to comment.