Skip to content

Commit 34a1472

Browse files
committed
allow using fallback seo
1 parent 14af415 commit 34a1472

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

config/inspirecms.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@
434434
'segment_provider' => DefaultSegmentProvider::class,
435435
'preview_provider' => DefaultPreviewProvider::class,
436436
'slug_generator' => DefaultSlugGenerator::class,
437+
'fallback_seo' => [
438+
'title' => env('APP_NAME', 'Home'),
439+
'description' => 'Powered by InspireCMS',
440+
'keywords' => '',
441+
'image' => null,
442+
],
437443
],
438444

439445
'sitemap' => [

src/Dtos/ContentDto.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,28 @@ public function getSeo(?string $locale = null)
217217
{
218218
$seo = collect($this->seo);
219219

220-
return $seo->get($locale ?? $this->getLocale()) ?? $seo->get($this->getFallbackLocale());
220+
$locale ??= $this->getLocale();
221+
222+
$result = $seo->get($locale);
223+
224+
// Using fallback locale
225+
if (! $result) {
226+
if (($fallbackLocale = $this->getFallbackLocale()) && ($fallbackSeo = $seo->get($fallbackLocale))) {
227+
$fallbackSeo->locale = $fallbackLocale;
228+
$result = $fallbackSeo;
229+
}
230+
} else {
231+
$result->locale = $locale;
232+
}
233+
234+
// Using default SEO data if not found
235+
if (! $result) {
236+
$result = SeoDto::fromArray([
237+
'locale' => $locale,
238+
]);
239+
}
240+
241+
return $result;
221242
}
222243

223244
public function isRedirectable(): bool

src/Dtos/SeoDto.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Contracts\Support\Htmlable;
66
use Illuminate\Support\Collection;
77
use Illuminate\Support\HtmlString;
8+
use SolutionForest\InspireCms\InspireCmsConfig;
89
use SolutionForest\InspireCms\Support\Base\Dtos\BaseDto;
910

1011
class SeoDto extends BaseDto
@@ -259,6 +260,29 @@ public static function fromArray(array $parameters)
259260

260261
$dto = parent::fromArray($parameters);
261262

263+
$fallbackSeo = InspireCmsConfig::get('frontend.fallback_seo', []);
264+
$fallbackExtraMapper = [
265+
'title' => ['ogTitle'],
266+
'description' => ['ogDescription'],
267+
'image' => ['ogImage'],
268+
];
269+
if (is_array($fallbackSeo) && ! empty($fallbackSeo)) {
270+
foreach ($fallbackSeo as $key => $value) {
271+
$keysToMap = array_unique(array_merge(
272+
[$key],
273+
$fallbackExtraMapper[$key] ?? []
274+
));
275+
foreach ($keysToMap ?? [] as $mappedKey) {
276+
if (! property_exists($dto, $mappedKey)) {
277+
continue;
278+
}
279+
if (! isset($dto->{$mappedKey}) || empty($dto->{$mappedKey})) {
280+
$dto->{$mappedKey} = $value;
281+
}
282+
}
283+
}
284+
}
285+
262286
return $dto;
263287
}
264288

0 commit comments

Comments
 (0)