Skip to content

Commit

Permalink
Improves canonical
Browse files Browse the repository at this point in the history
  • Loading branch information
BenParizek committed May 18, 2024
1 parent 1ca44d6 commit 168b6d6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG/CHANGELOG-META.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

## 4.2.4 - UNRELEASED

### Added

- Added Canonical URL Meta Type

### Fixed

- Fixed metadata field template paths for case sensitive systems ([#323])
- Fixed optimized title and description fallback logic when template is null
- Fixed bug where canonical URL was not respected as an override

[#323]: https://github.com/barrelstrength/sprout/issues/323

Expand Down
44 changes: 44 additions & 0 deletions src/meta/components/meta/CanonicalUrlMetaType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace BarrelStrength\Sprout\meta\components\meta;

use BarrelStrength\Sprout\meta\metadata\MetaType;
use Craft;
use craft\base\Field;

class CanonicalUrlMetaType extends MetaType
{
protected ?string $canonical = null;

public static function displayName(): string
{
return Craft::t('sprout-module-meta', 'Canonical URL');
}

public function attributes(): array
{
$attributes = parent::attributes();
$attributes[] = 'canonical';

return $attributes;
}

public function getCanonical(): ?string
{
if ($this->canonical || $this->metadata->getRawDataOnly()) {
return $this->canonical ?? $this->metadata->getCanonical();
}

return $this->metadata->getCanonical();
}

public function setCanonical(?string $value): void
{
$this->canonical = $value;
}

public function getHandle(): string
{
return 'canonical';
}
}
18 changes: 0 additions & 18 deletions src/meta/components/meta/RobotsMetaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

class RobotsMetaType extends MetaType
{
protected ?string $canonical = null;

protected string|array|null $robots = null;

public static function displayName(): string
Expand All @@ -21,27 +19,11 @@ public static function displayName(): string
public function attributes(): array
{
$attributes = parent::attributes();
$attributes[] = 'canonical';
$attributes[] = 'robots';

return $attributes;
}

public function getCanonical(): ?string
{
if ($this->canonical || $this->metadata->getRawDataOnly()) {

return $this->canonical;
}

return $this->metadata->getCanonical();
}

public function setCanonical(?string $value): void
{
$this->canonical = $value;
}

public function getRobots()
{
if ($this->robots || $this->metadata->getRawDataOnly()) {
Expand Down
2 changes: 2 additions & 0 deletions src/meta/metadata/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BarrelStrength\Sprout\meta\metadata;

use BarrelStrength\Sprout\meta\components\meta\CanonicalUrlMetaType;
use BarrelStrength\Sprout\meta\components\meta\GeoMetaType;
use BarrelStrength\Sprout\meta\components\meta\OpenGraphMetaType;
use BarrelStrength\Sprout\meta\components\meta\RobotsMetaType;
Expand Down Expand Up @@ -132,6 +133,7 @@ protected function setMetaTypes(array &$config = []): void
new TwitterMetaType([], $this),
new GeoMetaType([], $this),
new RobotsMetaType([], $this),
new CanonicalUrlMetaType([], $this),
];

foreach ($metaTypes as $metaType) {
Expand Down
7 changes: 4 additions & 3 deletions src/meta/schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BarrelStrength\Sprout\meta\schema;

use BarrelStrength\Sprout\forms\components\formfields\PhoneFormFieldData;
use BarrelStrength\Sprout\meta\components\meta\CanonicalUrlMetaType;
use BarrelStrength\Sprout\meta\components\meta\OpenGraphMetaType;
use BarrelStrength\Sprout\meta\components\meta\RobotsMetaType;
use BarrelStrength\Sprout\meta\components\schema\ContactPointSchema;
Expand Down Expand Up @@ -478,15 +479,15 @@ public function addMainEntityOfPage(): void
$meta = $this->prioritizedMetadataModel;

/** the prioritizedMetadataModel only has raw data, so we want the specific MetaType before we get our data */
$robots = $meta->getMetaType('robots');
$metaType = $meta->getMetaType('canonical');

if (!$robots instanceof RobotsMetaType) {
if (!$metaType instanceof CanonicalUrlMetaType) {
return;
}

$mainEntity = new MainEntityOfPageSchema();
$mainEntity->type = 'WebPage';
$mainEntity->id = $robots->getCanonical();
$mainEntity->id = $metaType->getCanonical();

$mainEntity->prioritizedMetadataModel = $this->prioritizedMetadataModel;

Expand Down

0 comments on commit 168b6d6

Please sign in to comment.