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..9ae0bef --- /dev/null +++ b/src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php @@ -0,0 +1,41 @@ +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..db58558 100644 --- a/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php +++ b/src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php @@ -14,9 +14,11 @@ /** * @GraphQLField( + * secure = true, * id = "url_metatags", * name = "metatags", * type = "[Metatag]", + * description = @Translation("Loads metatags for the URL."), * parents = {"InternalUrl", "EntityCanonicalUrl"} * ) */ @@ -72,8 +74,10 @@ 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']); + + $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..ed931bf --- /dev/null +++ b/src/Plugin/GraphQL/Fields/InternalUrl/SchemaMetatags.php @@ -0,0 +1,48 @@ +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..1fa9767 --- /dev/null +++ b/src/Plugin/GraphQL/Fields/Metatag/Content.php @@ -0,0 +1,28 @@ +