Skip to content

Commit

Permalink
Link related Statamic Pages (#66)
Browse files Browse the repository at this point in the history
* Fix

* removeSection or removeTab

* First commit
  • Loading branch information
martink635 authored May 9, 2024
1 parent 3419507 commit 224b3c2
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Generates the whole array of SEO settings:
...
```

# Version 4.1 changes

Seotamic v4.1 is a minor update that adds related meta tags. The related meta tags are used to link to other pages that are related to the current page in a multisite scenario. They are available as tags and can be used in the Antlers or Blade templates. In the PRO query mode, the related meta tags are also available in the REST API and GraphQL.

# Version 4 changes

Statamic v4 compatibility. Some internal functions were changed due to how blueprints work in Statamic v4. This release breaks compatibility with Statamic v3. Upgrade to Statamic v4 before upgrading to this version. Upgrade path from SEOtamic v2 is still the same.
Expand Down Expand Up @@ -93,6 +97,7 @@ If you need more control you can manually get each part of the output by using:
{{ seotamic:meta:description }}
{{ seotamic:meta:canonical }}
{{ seotamic:meta:robots }}
{{ seotamic:meta:related }}
```

This will return strings, so you need to wrap them in the appropriate tags, ie:
Expand Down
3 changes: 2 additions & 1 deletion resources/views/all.antlers.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
{{ partial src="seotamic::partials/canonical" }}
{{ partial src="seotamic::partials/og" }}
{{ partial src="seotamic::partials/twitter" }}
{{ partial src="seotamic::partials/robots" }}
{{ partial src="seotamic::partials/robots" }}
{{ partial src="seotamic::partials/related" }}
5 changes: 5 additions & 0 deletions resources/views/partials/_related.antlers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{ if meta:related }}
{{ meta:related }}
<link rel="alternate" hreflang="{{ lang }}" href="{{ href }}" />
{{ /meta:related }}
{{ /if }}
28 changes: 27 additions & 1 deletion src/FieldTypes/SeotamicMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Cnj\Seotamic\FieldTypes;

use Cnj\Seotamic\Http\Controllers\SitemapController;
use Illuminate\Support\Collection;

class SeotamicMeta extends SeotamicType
{
public function preProcess(mixed $data): array
Expand Down Expand Up @@ -66,7 +69,8 @@ public function augment($value): array
'title' => $this->getTitle(),
'description' => $value['description']['value'] ?? '',
'canonical' => $this->getCanonical(),
'robots' => $robots_none ? 'nofollow,noindex' : ''
'robots' => $robots_none ? 'nofollow,noindex' : '',
'related' => $this->getRelatedPages(),
];

if (isset($value['title']) && isset($value['title']['value'])) {
Expand Down Expand Up @@ -132,4 +136,26 @@ protected function getCanonical(): string

return $url ?? "";
}

/**
* Returns an array of related/alternate pages
*
* This pages are linked but available in different languages/options
*
* @return array
*/
protected function getRelatedPages(): ?Collection
{
if ($this->field->parent() instanceof \Statamic\Entries\Collection) {
return null;
}

$sitemapEntry = SitemapController::sitemapEntry($this->field->parent());

if (!$sitemapEntry && !isset($sitemapEntry['alternates'])) {
return null;
}

return $sitemapEntry['alternates'];
}
}
5 changes: 5 additions & 0 deletions src/GraphQL/SeotamicMetaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function fields(): array
'description' => 'Robots meta tag value',
'resolve' => $this->resolver()
],
'related' => [
'type' => GraphQL::string(),
'description' => 'Related pages',
'resolve' => $this->resolver()
],
];
}

Expand Down
8 changes: 4 additions & 4 deletions src/Http/Controllers/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function () use ($entry) {
);
}

private static function sitemapEntry(Entry $entry)
public static function sitemapEntry(Entry $entry)
{
return Cache::rememberForever(
'seotamic_sitemap_entry' . $entry->id(),
Expand All @@ -70,15 +70,15 @@ function () use ($entry) {
'lastmod' => $entry->lastModified()->toAtomString(),
'alternates' => $entry->sites()
->map(fn ($site) => $entry->in($site))
->filter(function (?Entry $entry) {
return $entry && self::shouldBeIndexed($entry);
->filter(function (?Entry $page) use ($entry) {
return $page && self::shouldBeIndexed($page) && $page->id() !== $entry->id();
})
->map(function (Entry $entry) {
return array(
'lang' => $entry->locale,
'href' => self::entryAbsoluteUrl($entry)
);
})
})->values()
];
}
);
Expand Down

0 comments on commit 224b3c2

Please sign in to comment.