Skip to content

Commit f332635

Browse files
committed
feat: Enhance SEO handling by adding root SEO context and update ContentHelper for ancestor web settings
1 parent 4ef8529 commit f332635

3 files changed

Lines changed: 47 additions & 8 deletions

File tree

src/Dtos/ContentDto.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,21 @@ protected static function prepareDtoParameters(Model $record, array $propertyDat
242242

243243
$dtoParameters = $record->toArray();
244244

245-
$dtoParameters['seo'] = collect($availableLanguages)->keys()->mapWithKeys(fn ($locale) => [
246-
$locale => $record->webSetting?->toDto($locale),
247-
])->all();
245+
$dtoParameters['seo'] = collect($availableLanguages)->keys()->mapWithKeys(function ($locale) use ($record) {
246+
$seo = $record->webSetting?->toDto($locale);
247+
248+
if (
249+
$seo instanceof SeoDto
250+
&& ($root = collect($record->ancestorsAndSelf)->where(fn ($item) => $item->getKey() !== $record->getKey())->last())
251+
&& ($rootSeo = $root->webSetting?->toDto($locale))
252+
) {
253+
$seo->setRoot($rootSeo);
254+
}
255+
256+
return [
257+
$locale => $seo,
258+
];
259+
})->all();
248260

249261
$dtoParameters['urls'] = collect($availableLanguages)->mapWithKeys(fn (LanguageDto $lang) => [
250262
$lang->code => $record->getUrl($lang),

src/Dtos/SeoDto.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ class SeoDto extends BaseDto
211211
*/
212212
public $noCache;
213213

214+
/**
215+
* @var self|null
216+
*/
217+
protected $root = null;
218+
214219
public static function fromArray(array $parameters)
215220
{
216221
$mapper = [
@@ -256,6 +261,17 @@ public static function fromArray(array $parameters)
256261
return $dto;
257262
}
258263

264+
/**
265+
* @param self|null $root
266+
* @return self
267+
*/
268+
public function setRoot($root)
269+
{
270+
$this->root = $root;
271+
272+
return $this;
273+
}
274+
259275
public function getHtml(): Htmlable
260276
{
261277
return new HtmlString($this->__toString());
@@ -266,7 +282,11 @@ public function __toString(): string
266282
$html = '';
267283

268284
if ($this->title) {
269-
$html .= "<title>{$this->title}</title>\n";
285+
$title = $this->title;
286+
if ($this->root) {
287+
$title .= ' - ' . $this->root->title;
288+
}
289+
$html .= "<title>{$title}</title>\n";
270290
}
271291

272292
if ($this->description) {
@@ -277,12 +297,18 @@ public function __toString(): string
277297
$html .= "<meta name=\"keywords\" content=\"{$this->keywords}\">\n";
278298
}
279299

280-
if ($this->ogTitle) {
281-
$html .= "<meta property=\"og:title\" content=\"{$this->ogTitle}\">\n";
300+
if ($this->ogTitle || $this->title) {
301+
$ogTitle = $this->ogTitle ?: $this->title;
302+
if ($this->root) {
303+
$ogTitle .= ' - ' . $this->root->title;
304+
}
305+
306+
$html .= "<meta property=\"og:title\" content=\"{$ogTitle}\">\n";
282307
}
283308

284-
if ($this->ogDescription) {
285-
$html .= "<meta property=\"og:description\" content=\"{$this->ogDescription}\">\n";
309+
if ($this->ogDescription || $this->description) {
310+
$ogDescription = $this->ogDescription ?: $this->description;
311+
$html .= "<meta property=\"og:description\" content=\"{$ogDescription}\">\n";
286312
}
287313

288314
if ($this->ogImage && ($mediaAssetUrl = inspirecms_asset()->findByKeys($this->ogImage)->first()?->getUrl())) {

src/Helpers/ContentHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public static function getDtoRequiredRelations(): array
9494
'webSetting',
9595
'publishedVersions',
9696
'templates',
97+
'ancestorsAndSelf.webSetting',
9798
];
9899
}
99100

0 commit comments

Comments
 (0)