Skip to content
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

saveElement() $saveContent changes #15393

Merged
merged 7 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# Release Notes for Craft CMS 5.4 (WIP)

### Extensibility
- `craft\services\Elements::saveContent()`’ now saves dirty fields’ content even if `$saveContent` is `false`. ([#15393](https://github.com/craftcms/cms/pull/15393))
2 changes: 1 addition & 1 deletion src/queue/jobs/ApplyNewPropagationMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ protected function processItem(mixed $item): void
$item->resaving = true;

try {
$elementsService->saveElement($item, updateSearchIndex: false);
$elementsService->saveElement($item, updateSearchIndex: false, saveContent: true);
} catch (Throwable $e) {
Craft::$app->getErrorHandler()->logException($e);
}
Expand Down
1 change: 1 addition & 0 deletions src/queue/jobs/ResaveElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ protected function processItem(mixed $item): void
Craft::$app->getElements()->saveElement($item,
updateSearchIndex: $this->updateSearchIndex,
forceTouch: $this->touch,
saveContent: true,
);
} catch (Throwable $e) {
Craft::$app->getErrorHandler()->logException($e);
Expand Down
33 changes: 22 additions & 11 deletions src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ public function ensureBulkOp(callable $callback): void
* @param bool $forceTouch Whether to force the `dateUpdated` timestamp to be updated for the element,
* regardless of whether it’s being resaved
* @param bool|null $crossSiteValidate Whether the element should be validated across all supported sites
* @param bool $saveContent Whether the element’s content should be saved
* @param bool $saveContent Whether all the element’s content should be saved. When false (default) only dirty fields will be saved.
* @return bool
* @throws ElementNotFoundException if $element has an invalid $id
* @throws Exception if the $element doesn’t have any supported sites
Expand All @@ -1270,7 +1270,7 @@ public function saveElement(
?bool $updateSearchIndex = null,
bool $forceTouch = false,
?bool $crossSiteValidate = false,
bool $saveContent = true,
bool $saveContent = false,
): bool {
// Force propagation for new elements
$propagate = !$element->id || $propagate;
Expand Down Expand Up @@ -1574,7 +1574,7 @@ public function resaveElements(

if ($e === null) {
try {
$this->_saveElementInternal($element, true, true, $updateSearchIndex, forceTouch: $touch);
$this->_saveElementInternal($element, true, true, $updateSearchIndex, forceTouch: $touch, saveContent: true);
} catch (Throwable $e) {
if (!$continueOnError) {
throw $e;
Expand Down Expand Up @@ -1861,7 +1861,7 @@ public function duplicateElement(
$transaction = Craft::$app->getDb()->beginTransaction();
try {
// Start with $element’s site
if (!$this->_saveElementInternal($mainClone, false, false, null, $supportedSites)) {
if (!$this->_saveElementInternal($mainClone, false, false, null, $supportedSites, saveContent: true)) {
throw new InvalidElementException($mainClone, 'Element ' . $element->id . ' could not be duplicated for site ' . $element->siteId);
}

Expand Down Expand Up @@ -1952,7 +1952,7 @@ public function duplicateElement(
}
}

if (!$this->_saveElementInternal($siteClone, false, false, supportedSites: $supportedSites)) {
if (!$this->_saveElementInternal($siteClone, false, false, supportedSites: $supportedSites, saveContent: true)) {
throw new InvalidElementException($siteClone, "Element $element->id could not be duplicated for site $siteElement->siteId: " . implode(', ', $siteClone->getFirstErrors()));
}

Expand Down Expand Up @@ -3435,7 +3435,7 @@ public function propagateElement(
* @param bool $forceTouch Whether to force the `dateUpdated` timestamp to be updated for the element,
* regardless of whether it’s being resaved
* @param bool $crossSiteValidate Whether the element should be validated across all supported sites
* @param bool $saveContent Whether the element’s content should be saved
* @param bool $saveContent Whether all the element’s content should be saved. When false (default) only dirty fields will be saved.
* @return bool
* @throws ElementNotFoundException if $element has an invalid $id
* @throws UnsupportedSiteException if the element is being saved for a site it doesn’t support
Expand All @@ -3449,7 +3449,7 @@ private function _saveElementInternal(
?array $supportedSites = null,
bool $forceTouch = false,
bool $crossSiteValidate = false,
bool $saveContent = true,
bool $saveContent = false,
): bool {
/** @var ElementInterface|DraftBehavior|RevisionBehavior $element */
$isNewElement = !$element->id;
Expand Down Expand Up @@ -3712,19 +3712,30 @@ private function _saveElementInternal(
}
}

if ($saveContent) {
// Set the field values
// if we're supposed to save all the content
if ($saveContent || !empty($dirtyFields)) {
$oldContent = $siteSettingsRecord->content; // we'll need that if we're not saving all the content
$content = [];
if ($fieldLayout) {
foreach ($fieldLayout->getCustomFields() as $field) {
if ($field::dbType() !== null) {
if (($saveContent || in_array($field->handle, $dirtyFields)) && $field::dbType() !== null) {
$serializedValue = $field->serializeValue($element->getFieldValue($field->handle), $element);
if ($serializedValue !== null) {
$content[$field->layoutElement->uid] = $serializedValue;
} elseif (!$saveContent) {
// if serialized value is null, and we're not saving all the content,
// we need to register the fact that the new value is empty
unset($oldContent[$field->layoutElement->uid]);
}
}
}
}

// if we're only saving dirty fields, we need to merge the new dirty values with what's already in the db
if (!$saveContent && $oldContent) {
$content = $content + $oldContent;
}

$siteSettingsRecord->content = $content ?: null;
}

Expand Down Expand Up @@ -3775,7 +3786,7 @@ private function _saveElementInternal(
$siteId,
$siteElement,
crossSiteValidate: $runValidation && $crossSiteValidate,
saveContent: $saveContent,
saveContent: true,
)) {
throw new InvalidConfigException();
}
Expand Down