Skip to content

Commit

Permalink
avoid introducing a new public method
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed Nov 10, 2023
1 parent 1196da0 commit 90b6246
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/Entries/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Entries;

use ArrayAccess;
use Closure;
use Facades\Statamic\Entries\InitiatorStack;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Responsable;
Expand Down Expand Up @@ -720,8 +721,11 @@ public function makeLocalization($site)
->origin($this)
->locale($site)
->published($this->published)
->slug($this->slug())
->addToStructure($this->parent());
->slug($this->slug());

if ($callback = $this->addToStructure($site, $this->parent())) {
$localization->afterSave($callback);
}

if ($this->collection()->dated()) {
$localization->date($this->date());
Expand All @@ -730,30 +734,30 @@ public function makeLocalization($site)
return $localization;
}

public function addToStructure($parent = null)
private function addToStructure($site, $parent = null): ?Closure
{
// If it's orderable (linear - a max depth of 1) then don't add it.
if ($this->collection()->orderable()) {
return $this;
return null;
}

// Collection not structured? Don't add it.
if (! $structure = $this->collection()->structure()) {
return $this;
return null;
}

$tree = $structure->in($this->locale());
$parent = optional($parent)->in($this->locale());
$tree = $structure->in($site);
$parent = optional($parent)->in($site);

$this->afterSave(function ($entry) use ($parent, $tree) {
return function ($entry) use ($parent, $tree) {
if (! $parent || $parent->isRoot()) {
$tree->append($entry);
} else {
$tree->appendTo($parent->id(), $entry);
}

$tree->save();
});

return $this;
};
}

public function supplementTaxonomies()
Expand Down

0 comments on commit 90b6246

Please sign in to comment.