Skip to content

Commit

Permalink
Don't resave elements that are missing their content
Browse files Browse the repository at this point in the history
Fixes #5030
  • Loading branch information
brandonkelly committed Oct 8, 2019
1 parent 1dcf0dc commit 72a4f23
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
- Craft no longer routes requests based on `action` params in the request body, if the request’s content type is `application/json`.
- Updated Twig to 2.12.

### Fixed
- Fixed a bug where the `resave/matrix-blocks` command would wittingly resave Matrix blocks even if they hadn’t been loaded with their content, resulting in lost content. ([#5030](https://github.com/craftcms/cms/issues/5030))

## 3.3.7 - 2019-10-03

### Changed
Expand Down
26 changes: 16 additions & 10 deletions src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,18 +527,24 @@ public function resaveElements(ElementQueryInterface $query, bool $continueOnErr
}

$e = null;

// Make sure this isn't a revision
if ($skipRevisions) {
try {
$root = ElementHelper::rootElement($element);
} catch (\Throwable $rootException) {
$root = null;
$e = new InvalidElementException($element, "Skipped resaving {$element} ({$element->id}) due to an error obtaining its root element: " . $rootException->getMessage());
try {
// Make sure the element was queried with its content
if ($element::hasContent() && $element->contentId === null) {
throw new InvalidElementException($element, "Skipped resaving {$element} ({$element->id}) because it wasn’t loaded with its content.");
}
if ($root && $root->getIsRevision()) {
$e = new InvalidElementException($element, "Skipped resaving {$element} ({$element->id}) because it's a revision.");

// Make sure this isn't a revision
if ($skipRevisions) {
try {
$root = ElementHelper::rootElement($element);
} catch (\Throwable $rootException) {
throw new InvalidElementException($element, "Skipped resaving {$element} ({$element->id}) due to an error obtaining its root element: " . $rootException->getMessage());
}
if ($root->getIsRevision()) {
throw new InvalidElementException($element, "Skipped resaving {$element} ({$element->id}) because it's a revision.");
}
}
} catch (InvalidElementException $e) {
}

if ($e === null) {
Expand Down

0 comments on commit 72a4f23

Please sign in to comment.