Skip to content
This repository has been archived by the owner on Nov 21, 2020. It is now read-only.

Add support for schema metatags #7

Open
wants to merge 6 commits into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Plugin/GraphQL/Fields/Entity/EntityMetatags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +18,7 @@
* id = "entity_metatags",
* name = "entityMetatags",
* type = "[Metatag]",
* description = @Translation("Loads metatags for the entity."),
* parents = {"Entity"}
* )
*/
Expand Down Expand Up @@ -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;
Expand Down
41 changes: 41 additions & 0 deletions src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Drupal\graphql_metatag\Plugin\GraphQL\Fields\Entity;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use GraphQL\Type\Definition\ResolveInfo;

/**
* @GraphQLField(
* secure = true,
* id = "entity_schema_metatags",
* name = "entitySchemaMetatags",
* type = "[SchemaMetatag]",
* description = @Translation("Loads schema.org defined metatags for the entity"),
* parents = {"Entity"}
* )
*/
class EntitySchemaMetatags extends EntityMetatags {

/**
* {@inheritdoc}
*/
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
if ($value instanceof ContentEntityInterface) {
$tags = $this->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;
}
}
}

}
10 changes: 7 additions & 3 deletions src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

/**
* @GraphQLField(
* secure = true,
* id = "url_metatags",
* name = "metatags",
* type = "[Metatag]",
* description = @Translation("Loads metatags for the URL."),
* parents = {"InternalUrl", "EntityCanonicalUrl"}
* )
*/
Expand Down Expand Up @@ -72,16 +74,18 @@ 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);
});

return function ($value, array $args, ResolveContext $context, ResolveInfo $info) use ($resolve) {
$tags = $resolve();
foreach ($tags as $tag) {
foreach ($tags->getValue() as $tag) {
yield $tag;
}
};
Expand Down
48 changes: 48 additions & 0 deletions src/Plugin/GraphQL/Fields/InternalUrl/SchemaMetatags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Drupal\graphql_metatag\Plugin\GraphQL\Fields\InternalUrl;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Url;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use GraphQL\Type\Definition\ResolveInfo;

/**
* @GraphQLField(
* secure = true,
* id = "url_schema_metatags",
* name = "schema_metatags",
* type = "[SchemaMetatag]",
* description = @Translation("Loads schema.org defined metatags for the URL"),
* parents = {"InternalUrl", "EntityCanonicalUrl"}
* )
*/
class SchemaMetatags extends Metatags {

/**
* {@inheritdoc}
*/
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
if ($value instanceof Url) {
$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) &&
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;
}
};
}
}

}
28 changes: 28 additions & 0 deletions src/Plugin/GraphQL/Fields/Metatag/Content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Drupal\graphql_metatag\Plugin\GraphQL\Fields\Metatag;

use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use GraphQL\Type\Definition\ResolveInfo;

/**
* @GraphQLField(
* secure = true,
* id = "metatag_content",
* name = "content",
* type = "String",
* description = @Translation("The JSON encoded content of a schema metatag"),
* parents = {"SchemaMetatag"}
* )
*/
class Content extends FieldPluginBase {

/**
* {@inheritdoc}
*/
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
yield json_encode($value['#attributes']['content']);
}

}
7 changes: 5 additions & 2 deletions src/Plugin/GraphQL/Fields/Metatag/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* id = "metatag_key",
* name = "key",
* type = "String",
* parents = {"Metatag"}
* parents = {"Metatag", "SchemaMetatag"}
* )
*/
class Key extends FieldPluginBase {
Expand All @@ -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'];
Expand Down
17 changes: 17 additions & 0 deletions src/Plugin/GraphQL/Interfaces/SchemaMetatag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Drupal\graphql_metatag\Plugin\GraphQL\Interfaces;

use Drupal\graphql\Plugin\GraphQL\Interfaces\InterfacePluginBase;

/**
* @GraphQLInterface(
* id = "schema_meta_tag",
* name = "SchemaMetatag",
* type = "schema_metatag",
* description = @Translation("SchemaMetatag interface containing schema metatag properties.")
* )
*/
class SchemaMetatag extends InterfacePluginBase {

}
29 changes: 29 additions & 0 deletions src/Plugin/GraphQL/Types/MetaHttpEquiv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Drupal\graphql_metatag\Plugin\GraphQL\Types;

use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase;
use GraphQL\Type\Definition\ResolveInfo;

/**
* @GraphQLType(
* id = "meta_http_equiv",
* name = "MetaHttpEquiv",
* interfaces = {"Metatag"}
* )
*/
class MetaHttpEquiv extends TypePluginBase {

/**
* {@inheritdoc}
*/
public function applies($object, ResolveContext $context, ResolveInfo $info = NULL) {
if (isset($object['#tag']) && $object['#tag'] === 'meta') {
return array_key_exists('http-equiv', $object['#attributes']);
}

return FALSE;
}

}
30 changes: 30 additions & 0 deletions src/Plugin/GraphQL/Types/MetaItemProp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Drupal\graphql_metatag\Plugin\GraphQL\Types;

use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase;
use GraphQL\Type\Definition\ResolveInfo;

/**
* @GraphQLType(
* id = "meta_item_prop",
* name = "MetaItemProp",
* interfaces = {"Metatag"}
* )
*/
class MetaItemProp extends TypePluginBase {

/**
* {@inheritdoc}
*/
public function applies($object, ResolveContext $context, ResolveInfo $info = NULL) {
if (isset($object['#tag']) && $object['#tag'] === 'meta') {
return array_key_exists('itemprop', $object['#attributes']);
}

return FALSE;
}

}

4 changes: 3 additions & 1 deletion src/Plugin/GraphQL/Types/MetaValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class MetaValue extends TypePluginBase {
*/
public function applies($object, ResolveContext $context, ResolveInfo $info = NULL) {
if (isset($object['#tag']) && $object['#tag'] === 'meta') {
return !array_key_exists('property', $object['#attributes']);
return !array_key_exists('property', $object['#attributes'])
&& !array_key_exists('http-equiv', $object['#attributes'])
&& !array_key_exists('itemprop', $object['#attributes']);
}

return FALSE;
Expand Down
26 changes: 26 additions & 0 deletions src/Plugin/GraphQL/Types/SchemaMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Drupal\graphql_metatag\Plugin\GraphQL\Types;

use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase;
use GraphQL\Type\Definition\ResolveInfo;

/**
* @GraphQLType(
* id = "schema_meta",
* name = "SchemaMeta",
* description = @Translation("Container for schema metatag properties."),
* interfaces = {"SchemaMetatag"}
* )
*/
class SchemaMeta extends TypePluginBase {

/**
* {@inheritdoc}
*/
public function applies($object, ResolveContext $context, ResolveInfo $info = NULL) {
return TRUE;
}

}