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

Bugfix/15949 move entry to section provisional drafts #16014

Merged
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
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