Skip to content

Commit

Permalink
Ensure SEO blueprint section/fields are removed when SEO is disabled. C…
Browse files Browse the repository at this point in the history
…loses #108.
  • Loading branch information
jesseleite committed Sep 21, 2020
1 parent b34bfe0 commit d1ce429
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
20 changes: 20 additions & 0 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*
Expand Down
14 changes: 7 additions & 7 deletions src/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)
);
}

/**
Expand Down

0 comments on commit d1ce429

Please sign in to comment.