From ead2a0c4f5f7247e56cbb8b09a180389fbf7de78 Mon Sep 17 00:00:00 2001 From: Richard Le Poidevin Date: Thu, 3 Aug 2023 12:59:47 +0100 Subject: [PATCH] Schema.org improvements Allow Block `schemaOrg()` to return null so logic can be added Limit depth of components to check for schema to stop everything being returned Configure with: `storyblok.schema_org_depth` --- config/storyblok.php | 11 +++++++++++ src/Traits/SchemaOrg.php | 12 +++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/config/storyblok.php b/config/storyblok.php index 7387650..83501cd 100644 --- a/config/storyblok.php +++ b/config/storyblok.php @@ -218,4 +218,15 @@ | */ 'date_format' => 'H:i:s j F Y', + + /* + |-------------------------------------------------------------------------- + | How deep to go when creating page schema.org data + |-------------------------------------------------------------------------- + | + | As you may be nesting many blocks and linking to other stories, this + | you may want to limit the depth of the schema.org data returned + | + */ + 'schema_org_depth' => 5, ]; diff --git a/src/Traits/SchemaOrg.php b/src/Traits/SchemaOrg.php index 6b52788..73bedbc 100644 --- a/src/Traits/SchemaOrg.php +++ b/src/Traits/SchemaOrg.php @@ -20,8 +20,8 @@ protected function initSchemaOrg(): void $page = $this->page(); } - if ($page) { - $this->add($page); + if ($page && count($this->_componentPath) <= config('storyblok.schema_org_depth')) { + $this->addschemaOrg($page); } } @@ -48,12 +48,14 @@ public function schemaOrgScript(): string * * @param $page */ - private function add($page): void + protected function addschemaOrg($page): void { $currentSchemaOrg = $page->meta('schema_org'); - $currentSchemaOrg[] = $this->schemaOrg(); + if ($schema = $this->schemaOrg()) { + $currentSchemaOrg[] = $schema; + } $page->replaceMeta('schema_org', $currentSchemaOrg ?? []); } -} \ No newline at end of file +}