Skip to content

Commit

Permalink
Merge pull request #16014 from craftcms/bugfix/15949-move-entry-to-se…
Browse files Browse the repository at this point in the history
…ction-provisional-drafts

Bugfix/15949 move entry to section provisional drafts
  • Loading branch information
brandonkelly authored Nov 6, 2024
2 parents 767b2b9 + 1bead3c commit 89bb52d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Fixed a bug where Number fields weren’t getting sorted properly in PostgreSQL. ([#15973](https://github.com/craftcms/cms/issues/15973))
- Fixed a bug where field conditions weren’t taking effect within Matrix fields set to inline-editable blocks mode, if `autosaveDrafts` was disabled. ([#15985](https://github.com/craftcms/cms/issues/15985))
- Fixed an error that occurred when attempting to delete a nested Matrix entry, if it had an entry type that was no longer allowed for the Matrix field. ([#15990](https://github.com/craftcms/cms/issues/15990))
- Fixed a bug where structure data wasn’t getting deleted for drafts when moving an entry out of a Structure section. ([#15949](https://github.com/craftcms/cms/issues/15949), [#16014](https://github.com/craftcms/cms/pull/16014))
- Updated Axios to 1.7.7. ([#15958](https://github.com/craftcms/cms/issues/15958))

## 5.4.9 - 2024-10-22
Expand Down
58 changes: 42 additions & 16 deletions src/services/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -1986,34 +1986,60 @@ public function moveEntryToSection(Entry $entry, Section $section): bool
throw new InvalidElementException($entry, 'Element ' . $entry->id . ' could not be moved for site ' . $entry->siteId);
}

$structuresService = Craft::$app->getStructures();
$draftsQuery = Entry::find()
->draftOf($entry)
->provisionalDrafts(null)
->status(null)
->site('*')
->unique();

$revisionsQuery = Entry::find()
->revisionOf($entry)
->status(null)
->site('*')
->unique();

if (
$entry->getIsCanonical() &&
in_array(Section::TYPE_STRUCTURE, [$oldSection->type, $section->type])
) {
$structuresService = Craft::$app->getStructures();

if ($entry->getIsCanonical()) {
$canonical = $entry->getCanonical(true);
// if we're moving it from a Structure section, remove it from the structure
if ($oldSection->type === Section::TYPE_STRUCTURE) {
$structuresService->remove($oldSection->structureId, $entry);

// remove drafts and revisions from the structure, too
foreach (Db::each($draftsQuery) as $draft) {
/** @var Entry $draft */
if ($draft->lft) {
$structuresService->remove($oldSection->structureId, $draft);
}
}

foreach (Db::each($revisionsQuery) as $revision) {
/** @var Entry $revision */
if ($revision->lft) {
$structuresService->remove($oldSection->structureId, $revision);
}
}
}

// if we're moving it to a Structure section, place it at the root
if ($section->type === Section::TYPE_STRUCTURE && $canonical->structureId) {
if ($section->type === Section::TYPE_STRUCTURE) {
if ($section->defaultPlacement === Section::DEFAULT_PLACEMENT_BEGINNING) {
$structuresService->prependToRoot($section->structureId, $canonical, Structures::MODE_INSERT);
$structuresService->prependToRoot($section->structureId, $entry, Structures::MODE_INSERT);
} else {
$structuresService->appendToRoot($section->structureId, $canonical, Structures::MODE_INSERT);
$structuresService->appendToRoot($section->structureId, $entry, Structures::MODE_INSERT);
}
}

// if we're moving it from a Structure section, remove it from the structure
if ($oldSection->structureId) {
$structuresService->remove($oldSection->structureId, $canonical);
}
}

$entry->newSiteIds = [];
$entry->afterPropagate(false);

// now update drafts & revisions too
$ids = array_merge(
Entry::find()->draftOf($entry)->status(null)->site('*')->unique()->ids(),
Entry::find()->revisionOf($entry)->status(null)->site('*')->unique()->ids(),
);
// now assign drafts & revisions to the new section too
$ids = array_merge($draftsQuery->ids(), $revisionsQuery->ids());
if (!empty($ids)) {
Db::update(Table::ENTRIES, [
'sectionId' => $section->id,
Expand Down

0 comments on commit 89bb52d

Please sign in to comment.