Skip to content

Commit 0aa6cd8

Browse files
committed
fix: Update SEO handling to set ancestors instead of root for better context management
1 parent 6617652 commit 0aa6cd8

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/Dtos/ContentDto.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,9 @@ protected static function prepareDtoParameters(Model $record, array $propertyDat
247247

248248
if (
249249
$seo instanceof SeoDto
250-
&& ($root = collect($record->ancestorsAndSelf)->where(fn ($item) => $item->getKey() !== $record->getKey())->last())
251-
&& ($rootSeo = $root->webSetting?->toDto($locale))
250+
&& ($ancestors = collect($record->ancestorsAndSelf)->where(fn ($item) => $item->getKey() !== $record->getKey())->pluck('webSetting')->map(fn ($item) => $item?->toDto($locale)))
252251
) {
253-
$seo->setRoot($rootSeo);
252+
$seo->setAncestors($ancestors);
254253
}
255254

256255
return [

src/Dtos/SeoDto.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SolutionForest\InspireCms\Dtos;
44

55
use Illuminate\Contracts\Support\Htmlable;
6+
use Illuminate\Support\Collection;
67
use Illuminate\Support\HtmlString;
78
use SolutionForest\InspireCms\Support\Base\Dtos\BaseDto;
89

@@ -212,9 +213,9 @@ class SeoDto extends BaseDto
212213
public $noCache;
213214

214215
/**
215-
* @var self|null
216+
* @var Collection<self>|null
216217
*/
217-
protected $root = null;
218+
protected $ancestors = null;
218219

219220
public static function fromArray(array $parameters)
220221
{
@@ -262,12 +263,15 @@ public static function fromArray(array $parameters)
262263
}
263264

264265
/**
265-
* @param self|null $root
266+
* @param self[]|Collection<slef>|null $ancestors
266267
* @return self
267268
*/
268-
public function setRoot($root)
269+
public function setAncestors($ancestors)
269270
{
270-
$this->root = $root;
271+
$this->ancestors = collect($ancestors ?? [])
272+
->whereInstanceOf(static::class)
273+
->filter()
274+
->values();
271275

272276
return $this;
273277
}
@@ -281,10 +285,12 @@ public function __toString(): string
281285
{
282286
$html = '';
283287

288+
$parentSeo = $this->ancestors->filter()->last();
289+
284290
if ($this->title) {
285291
$title = $this->title;
286-
if ($this->root) {
287-
$title .= ' - ' . $this->root->title;
292+
if ($parentSeo) {
293+
$title .= ' - ' . $parentSeo->title;
288294
}
289295
$html .= "<title>{$title}</title>\n";
290296
}
@@ -299,8 +305,8 @@ public function __toString(): string
299305

300306
if ($this->ogTitle || $this->title) {
301307
$ogTitle = $this->ogTitle ?: $this->title;
302-
if ($this->root) {
303-
$ogTitle .= ' - ' . $this->root->title;
308+
if ($parentSeo) {
309+
$ogTitle .= ' - ' . $parentSeo->title;
304310
}
305311

306312
$html .= "<meta property=\"og:title\" content=\"{$ogTitle}\">\n";

0 commit comments

Comments
 (0)