From d1ce429e42bf4419b1b9f5b8dc71937ed8e07292 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Mon, 21 Sep 2020 16:21:00 -0400 Subject: [PATCH] Ensure SEO blueprint section/fields are removed when SEO is disabled. Closes #108. --- src/Blueprint.php | 20 ++++++++++++++++++++ src/Subscriber.php | 14 +++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/Blueprint.php b/src/Blueprint.php index a3e66ed2..56ff99b2 100644 --- a/src/Blueprint.php +++ b/src/Blueprint.php @@ -38,6 +38,18 @@ public static function on($event) return new static($event); } + /** + * Ensure SEO section and fields are added to (or removed from) blueprint. + * + * @param bool $isEnabled + */ + public function ensureSeoFields($isEnabled = true) + { + $isEnabled + ? $this->addSeoFields() + : $this->removeSeoFields(); + } + /** * Add SEO section and fields to blueprint. */ @@ -54,6 +66,14 @@ public function addSeoFields() static::$addingField = false; } + /** + * Remove SEO section and fields from blueprint. + */ + public function removeSeoFields() + { + $this->blueprint->removeSection('SEO'); + } + /** * Get event data. * diff --git a/src/Subscriber.php b/src/Subscriber.php index c85a2e56..69494769 100644 --- a/src/Subscriber.php +++ b/src/Subscriber.php @@ -17,8 +17,8 @@ class Subscriber * @var array */ protected $events = [ - Events\EntryBlueprintFound::class => 'addSeoFields', - Events\TermBlueprintFound::class => 'addSeoFields', + Events\EntryBlueprintFound::class => 'ensureSeoFields', + Events\TermBlueprintFound::class => 'ensureSeoFields', Events\CollectionSaved::class => 'clearSitemapCache', Events\EntrySaved::class => 'clearSitemapCache', Events\TaxonomySaved::class => 'clearSitemapCache', @@ -38,15 +38,15 @@ public function subscribe($events) } /** - * Add SEO section and fields to entry blueprint. + * Ensure section blueprint has (or doesn't have) SEO fields. * * @param mixed $event */ - public function addSeoFields($event) + public function ensureSeoFields($event) { - if ($this->seoIsEnabledForSection($event)) { - Blueprint::on($event)->addSeoFields(); - } + Blueprint::on($event)->ensureSeoFields( + $this->seoIsEnabledForSection($event) + ); } /**