Skip to content
Open
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
21 changes: 12 additions & 9 deletions src/Actions/DuplicateEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,18 @@ private function duplicateEntry(Entry $original, ?string $origin = null)
$this->duplicateEntry($descendant, origin: $entry->id());
});

if ($originalParent && $originalParent !== $original->id()) {
$entry->structure()
->in($original->locale())
->appendTo($originalParent->id(), $entry)
->save();
if ($entry->structure()) {
if ($originalParent && $originalParent !== $original->id()) {
$entry->structure()
->in($original->locale())
->appendTo($originalParent->id(), $entry)
->save();
} else {
$entry->structure()
->in($original->locale())
->append($entry)
->save();
}
}

return $entry;
Expand All @@ -134,10 +141,6 @@ protected function getEntryParentFromStructure(Entry $entry)
return null;
}

if ($entry->structure()->expectsRoot() && $entry->structure()->in($entry->locale())->root()['entry'] === $parentEntry->id()) {
return null;
}

return $parentEntry;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/Actions/DuplicateEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Statamic\Facades\Entry;
use Statamic\Facades\Site;
use Statamic\Facades\User;
use Statamic\Structures\CollectionStructure;
use Tests\FakesRoles;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
Expand Down Expand Up @@ -558,6 +559,34 @@ public function if_an_entry_has_an_origin_and_the_root_origin_is_also_selected_i
], $this->entryData());
}

#[Test]
public function it_adds_duplicated_entry_to_tree()
{
$this->setSites([
'en' => ['url' => 'http://domain.com/', 'locale' => 'en'],
'fr' => ['url' => 'http://domain.com/fr/', 'locale' => 'fr'],
]);

$structure = new CollectionStructure;
Collection::make('test')->sites(['en', 'fr'])->structure($structure)->save();

$entry = EntryFactory::id('alfa-id')->collection('test')->slug('alfa')->data(['title' => 'Alfa'])->create();
$entry->makeLocalization('fr')->id('alfa-id-fr')->slug('alfa-fr')->data(['title' => 'Alfa (French)'])->save();

// Make super user since this test isn't concerned with permissions.
$this->actingAs(tap(User::make()->makeSuper())->save());

(new DuplicateEntry)->run(collect([
Entry::find('alfa-id-fr'),
Entry::find('alfa-id'),
]), []);

Entry::all()->each(function ($entry) use ($structure) {
$tree = $structure->in($entry->site());
$this->assertEquals($entry, $tree->withEntries()->entry($entry->id()));
});
}

private function entryData()
{
return Entry::all()->map(function ($entry) {
Expand Down
Loading