From e1bc0917991ba9f6b869a5e3f2a2573cdde168b5 Mon Sep 17 00:00:00 2001 From: Giovanni Buffa Date: Wed, 11 Oct 2023 15:17:12 +0200 Subject: [PATCH] Fix empty title on some cases --- resources/views/metatags.antlers.html | 6 ++++++ src/Tags/MetatagsTags.php | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/resources/views/metatags.antlers.html b/resources/views/metatags.antlers.html index 7d54485..df3297a 100644 --- a/resources/views/metatags.antlers.html +++ b/resources/views/metatags.antlers.html @@ -9,6 +9,12 @@ {{ /if }} +{{ unless settings | in_array:basic }} +{{ fields }} +{{ basic_title }} {{ extras.site_name_separator}} {{ extras.site_name}} +{{ /fields }} +{{ /unless }} + {{ if settings | in_array:advanced }} {{ fields }} diff --git a/src/Tags/MetatagsTags.php b/src/Tags/MetatagsTags.php index ce4665d..8594238 100644 --- a/src/Tags/MetatagsTags.php +++ b/src/Tags/MetatagsTags.php @@ -8,6 +8,7 @@ use Gioppy\StatamicMetatags\Settings; use Illuminate\Support\Str; use Statamic\Fields\Value; +use Statamic\Fieldtypes\Text; use Statamic\Support\Arr; use Statamic\Tags\Tags; @@ -45,9 +46,15 @@ public function index() { ->merge($pageFields); // Check if fields have title, otherwise load default title + if (array_key_exists('basic_title', $fields->all()) && !is_string($fields->only('basic_title')->all()['basic_title'])) { + $defaultTitle = $page->only('title')->all(); + $fields->prepend(new Value($defaultTitle['title'] ?? ' '), 'basic_title'); + } + + // If there is no basic_title, load page title if (!array_key_exists('basic_title', $fields->all())) { $defaultTitle = $page->only('title')->all(); - $fields->prepend(new Value($defaultTitle['title']), 'basic_title'); + $fields->prepend(new Value($defaultTitle['title'] ?? ' ', 'basic_title', new Text()), 'basic_title'); } return view('statamic-metatags::metatags', [