Skip to content

Commit

Permalink
Merge pull request #11 from gioppy/fix/title
Browse files Browse the repository at this point in the history
Fix empty title on some cases
  • Loading branch information
gioppy committed Oct 11, 2023
2 parents 5fa9296 + e1bc091 commit fa6a888
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 6 additions & 0 deletions resources/views/metatags.antlers.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<link rel="canonical" href="{{ advanced_canonical ?? extras.permalink }}">
{{ /if }}

{{ unless settings | in_array:basic }}
{{ fields }}
<title>{{ basic_title }} {{ extras.site_name_separator}} {{ extras.site_name}}</title>
{{ /fields }}
{{ /unless }}

{{ if settings | in_array:advanced }}
<!-- ADVANCED METATAGS -->
{{ fields }}
Expand Down
9 changes: 8 additions & 1 deletion src/Tags/MetatagsTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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', [
Expand Down

0 comments on commit fa6a888

Please sign in to comment.