From 0db2475015f39df80a7bfe5a2e103da154ee617c Mon Sep 17 00:00:00 2001 From: Mladen Todorovic Date: Wed, 29 Aug 2018 15:06:35 +0200 Subject: [PATCH 1/6] Add support for schema metatags --- .../GraphQL/Fields/Entity/EntityMetatags.php | 8 ++++ .../Fields/Entity/EntitySchemaMetatags.php | 40 ++++++++++++++++ .../GraphQL/Fields/InternalUrl/Metatags.php | 10 ++-- .../Fields/InternalUrl/SchemaMetatags.php | 47 +++++++++++++++++++ src/Plugin/GraphQL/Fields/Metatag/Content.php | 27 +++++++++++ src/Plugin/GraphQL/Fields/Metatag/Key.php | 2 +- .../GraphQL/Interfaces/SchemaMetatag.php | 17 +++++++ src/Plugin/GraphQL/Types/SchemaMeta.php | 26 ++++++++++ 8 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php create mode 100644 src/Plugin/GraphQL/Fields/InternalUrl/SchemaMetatags.php create mode 100644 src/Plugin/GraphQL/Fields/Metatag/Content.php create mode 100644 src/Plugin/GraphQL/Interfaces/SchemaMetatag.php create mode 100644 src/Plugin/GraphQL/Types/SchemaMeta.php diff --git a/src/Plugin/GraphQL/Fields/Entity/EntityMetatags.php b/src/Plugin/GraphQL/Fields/Entity/EntityMetatags.php index d47af22..6f0dde7 100644 --- a/src/Plugin/GraphQL/Fields/Entity/EntityMetatags.php +++ b/src/Plugin/GraphQL/Fields/Entity/EntityMetatags.php @@ -2,6 +2,7 @@ namespace Drupal\graphql_metatag\Plugin\GraphQL\Fields\Entity; +use Drupal\Component\Utility\NestedArray; use Drupal\Core\DependencyInjection\DependencySerializationTrait; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -17,6 +18,7 @@ * id = "entity_metatags", * name = "entityMetatags", * type = "[Metatag]", + * description = @Translation("Loads metatags for the entity."), * parents = {"Entity"} * ) */ @@ -69,7 +71,13 @@ public function __construct( protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { if ($value instanceof ContentEntityInterface) { $tags = $this->metatagManager->tagsFromEntityWithDefaults($value); + + // Filter non schema metatags, because schema metatags are processed in + // EntitySchemaMetatags class. $elements = $this->metatagManager->generateRawElements($tags, $value); + $elements = array_filter($elements, function ($metatag_object) { + return !NestedArray::getValue($metatag_object, ['#attributes', 'schema_metatag']); + }); foreach ($elements as $element) { yield $element; diff --git a/src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php b/src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php new file mode 100644 index 0000000..7d69a1c --- /dev/null +++ b/src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php @@ -0,0 +1,40 @@ +metatagManager->tagsFromEntityWithDefaults($value); + + // Process only schema metatags. + $elements = $this->metatagManager->generateRawElements($tags, $value); + $elements = array_filter($elements, function ($metatag_object) { + return NestedArray::getValue($metatag_object, ['#attributes', 'schema_metatag']) === TRUE; + }); + + foreach ($elements as $element) { + yield $element; + } + } + } + +} diff --git a/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php b/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php index f02e54b..72082c1 100644 --- a/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php +++ b/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php @@ -17,6 +17,7 @@ * id = "url_metatags", * name = "metatags", * type = "[Metatag]", + * description = @Translation("Loads metatags for the URL."), * parents = {"InternalUrl", "EntityCanonicalUrl"} * ) */ @@ -72,8 +73,11 @@ protected function resolveValues($value, array $args, ResolveContext $context, R $resolve = $this->subRequestBuffer->add($value, function () { $tags = metatag_get_tags_from_route(); $tags = NestedArray::getValue($tags, ['#attached', 'html_head']) ?: []; - $tags = array_filter($tags, function($tag) { - return is_array($tag) && in_array(NestedArray::getValue($tag, [0, '#tag']), ['meta', 'link']); + + // TODO: Filter non schema ones. + $tags = array_filter($tags, function ($tag) { + return is_array($tag) && + !NestedArray::getValue($tag, [0, '#attributes', 'schema_metatag']); }); return array_map('reset', $tags); @@ -81,7 +85,7 @@ protected function resolveValues($value, array $args, ResolveContext $context, R return function ($value, array $args, ResolveContext $context, ResolveInfo $info) use ($resolve) { $tags = $resolve(); - foreach ($tags as $tag) { + foreach ($tags->getValue() as $tag) { yield $tag; } }; diff --git a/src/Plugin/GraphQL/Fields/InternalUrl/SchemaMetatags.php b/src/Plugin/GraphQL/Fields/InternalUrl/SchemaMetatags.php new file mode 100644 index 0000000..66ccbca --- /dev/null +++ b/src/Plugin/GraphQL/Fields/InternalUrl/SchemaMetatags.php @@ -0,0 +1,47 @@ +subRequestBuffer->add($value, function () { + $tags = metatag_get_tags_from_route(); + $tags = NestedArray::getValue($tags, ['#attached', 'html_head']) ?: []; + + $tags = array_filter($tags, function ($tag) { + return is_array($tag) && + NestedArray::getValue($tag, [0, '#attributes', 'schema_metatag']) === TRUE; + }); + + return array_map('reset', $tags); + }); + + return function ($value, array $args, ResolveContext $context, ResolveInfo $info) use ($resolve) { + $tags = $resolve(); + foreach ($tags->getValue() as $tag) { + yield $tag; + } + }; + } + } + +} diff --git a/src/Plugin/GraphQL/Fields/Metatag/Content.php b/src/Plugin/GraphQL/Fields/Metatag/Content.php new file mode 100644 index 0000000..5bf5aab --- /dev/null +++ b/src/Plugin/GraphQL/Fields/Metatag/Content.php @@ -0,0 +1,27 @@ + Date: Wed, 29 Aug 2018 15:39:35 +0200 Subject: [PATCH 2/6] Fix for last minute change bug --- src/Plugin/GraphQL/Types/SchemaMeta.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/GraphQL/Types/SchemaMeta.php b/src/Plugin/GraphQL/Types/SchemaMeta.php index 97a35c5..b445a63 100644 --- a/src/Plugin/GraphQL/Types/SchemaMeta.php +++ b/src/Plugin/GraphQL/Types/SchemaMeta.php @@ -10,7 +10,7 @@ * @GraphQLType( * id = "schema_meta", * name = "SchemaMeta", - * description = @Translation("Container for schema metatag properties.") + * description = @Translation("Container for schema metatag properties."), * interfaces = {"SchemaMetatag"} * ) */ From e57ef37f83b2dd42fba1a85666097ccf671c5962 Mon Sep 17 00:00:00 2001 From: Mladen Todorovic Date: Wed, 19 Sep 2018 10:11:00 +0200 Subject: [PATCH 3/6] Removed obsolete comment --- src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php b/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php index 72082c1..aa52e1c 100644 --- a/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php +++ b/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php @@ -74,7 +74,6 @@ protected function resolveValues($value, array $args, ResolveContext $context, R $tags = metatag_get_tags_from_route(); $tags = NestedArray::getValue($tags, ['#attached', 'html_head']) ?: []; - // TODO: Filter non schema ones. $tags = array_filter($tags, function ($tag) { return is_array($tag) && !NestedArray::getValue($tag, [0, '#attributes', 'schema_metatag']); From 288f0ee4d0005e00a713bb44ac5ea2e50b950779 Mon Sep 17 00:00:00 2001 From: Timo Welde Date: Thu, 20 Sep 2018 13:18:38 +0200 Subject: [PATCH 4/6] made fields secure --- src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php | 1 + src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php | 1 + src/Plugin/GraphQL/Fields/InternalUrl/SchemaMetatags.php | 1 + src/Plugin/GraphQL/Fields/Metatag/Content.php | 1 + 4 files changed, 4 insertions(+) diff --git a/src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php b/src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php index 7d69a1c..9ae0bef 100644 --- a/src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php +++ b/src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php @@ -9,6 +9,7 @@ /** * @GraphQLField( + * secure = true, * id = "entity_schema_metatags", * name = "entitySchemaMetatags", * type = "[SchemaMetatag]", diff --git a/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php b/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php index aa52e1c..db58558 100644 --- a/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php +++ b/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php @@ -14,6 +14,7 @@ /** * @GraphQLField( + * secure = true, * id = "url_metatags", * name = "metatags", * type = "[Metatag]", diff --git a/src/Plugin/GraphQL/Fields/InternalUrl/SchemaMetatags.php b/src/Plugin/GraphQL/Fields/InternalUrl/SchemaMetatags.php index 66ccbca..ed931bf 100644 --- a/src/Plugin/GraphQL/Fields/InternalUrl/SchemaMetatags.php +++ b/src/Plugin/GraphQL/Fields/InternalUrl/SchemaMetatags.php @@ -9,6 +9,7 @@ /** * @GraphQLField( + * secure = true, * id = "url_schema_metatags", * name = "schema_metatags", * type = "[SchemaMetatag]", diff --git a/src/Plugin/GraphQL/Fields/Metatag/Content.php b/src/Plugin/GraphQL/Fields/Metatag/Content.php index 5bf5aab..1fa9767 100644 --- a/src/Plugin/GraphQL/Fields/Metatag/Content.php +++ b/src/Plugin/GraphQL/Fields/Metatag/Content.php @@ -8,6 +8,7 @@ /** * @GraphQLField( + * secure = true, * id = "metatag_content", * name = "content", * type = "String", From ec351db196267d7078ee74b9e312b29e6b1acd09 Mon Sep 17 00:00:00 2001 From: Mladen Todorovic Date: Mon, 19 Nov 2018 09:50:40 +0100 Subject: [PATCH 5/6] Support for http-equiv and itemprop meta tags --- src/Plugin/GraphQL/Fields/Metatag/Key.php | 5 +++- src/Plugin/GraphQL/Types/MetaHttpEquiv.php | 29 +++++++++++++++++++++ src/Plugin/GraphQL/Types/MetaItemProp.php | 30 ++++++++++++++++++++++ src/Plugin/GraphQL/Types/MetaValue.php | 4 ++- 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/Plugin/GraphQL/Types/MetaHttpEquiv.php create mode 100644 src/Plugin/GraphQL/Types/MetaItemProp.php diff --git a/src/Plugin/GraphQL/Fields/Metatag/Key.php b/src/Plugin/GraphQL/Fields/Metatag/Key.php index 25f8cc4..8650b2f 100644 --- a/src/Plugin/GraphQL/Fields/Metatag/Key.php +++ b/src/Plugin/GraphQL/Fields/Metatag/Key.php @@ -22,7 +22,10 @@ class Key extends FieldPluginBase { */ protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { if (isset($value['#tag']) && $value['#tag'] === 'meta') { - yield isset($value['#attributes']['property']) ? $value['#attributes']['property'] : $value['#attributes']['name']; + yield isset($value['#attributes']['property']) ? $value['#attributes']['property'] : + ((isset($value['#attributes']['http-equiv'])) ? $value['#attributes']['http-equiv'] : + ((isset($value['#attributes']['itemprop'])) ? $value['#attributes']['itemprop'] : $value['#attributes']['name']) + ); } else if (isset($value['#tag']) && $value['#tag'] === 'link') { yield $value['#attributes']['rel']; diff --git a/src/Plugin/GraphQL/Types/MetaHttpEquiv.php b/src/Plugin/GraphQL/Types/MetaHttpEquiv.php new file mode 100644 index 0000000..75633c7 --- /dev/null +++ b/src/Plugin/GraphQL/Types/MetaHttpEquiv.php @@ -0,0 +1,29 @@ + Date: Mon, 19 Nov 2018 10:15:14 +0100 Subject: [PATCH 6/6] Remove wrong part of code --- src/Plugin/GraphQL/Types/MetaItemProp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/GraphQL/Types/MetaItemProp.php b/src/Plugin/GraphQL/Types/MetaItemProp.php index 459909d..b329d59 100644 --- a/src/Plugin/GraphQL/Types/MetaItemProp.php +++ b/src/Plugin/GraphQL/Types/MetaItemProp.php @@ -27,4 +27,4 @@ public function applies($object, ResolveContext $context, ResolveInfo $info = NU } } -AcquiaLiftMetaTags.php \ No newline at end of file +