Skip to content

Commit

Permalink
Remove hreflang of canonicalized pages
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Jul 10, 2024
1 parent 3cd7610 commit a4ebfad
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/View/Concerns/HasHreflang.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait HasHreflang

protected function entryAndTermHreflang(Entry|LocalizedTerm $model): ?array
{
if (! $this->isIndexable($model)) {
if (! $this->shouldIncludeHreflang($model)) {
return null;
}

Expand All @@ -31,7 +31,7 @@ protected function entryAndTermHreflang(Entry|LocalizedTerm $model): ?array
$hreflang = $sites
->map(fn ($locale) => $model->in($locale))
->filter() // A model might not exist in a site. So we need to remove it to prevent calling methods on null
->filter($this->isIndexable(...));
->filter($this->shouldIncludeHreflang(...));

if ($hreflang->count() < 2) {
return null;
Expand All @@ -44,7 +44,7 @@ protected function entryAndTermHreflang(Entry|LocalizedTerm $model): ?array

$origin = $model->origin() ?? $model;

$xDefault = $this->isIndexable($origin) ? $origin : $model;
$xDefault = $this->shouldIncludeHreflang($origin) ? $origin : $model;

return $hreflang->push([
'url' => $this->absoluteUrl($xDefault),
Expand Down Expand Up @@ -136,4 +136,18 @@ protected function collectionTaxonomyUrl(Taxonomy $taxonomy, string $site): stri

return URL::tidy("{$siteUrl}/{$collectionHandle}/{$taxonomyHandle}");
}

protected function shouldIncludeHreflang(Entry|LocalizedTerm $model): bool
{
if ($this->canonicalPointsToAnotherUrl($model)) {
return false;
}

return $this->isIndexable($model);
}

protected function canonicalPointsToAnotherUrl(Entry|LocalizedTerm $model): bool
{
return $model->seo_canonical_type != 'current';
}
}

0 comments on commit a4ebfad

Please sign in to comment.