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

Allow modules to alter metatags #18

Open
wants to merge 1 commit 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
17 changes: 12 additions & 5 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 Down Expand Up @@ -68,11 +69,17 @@ public function __construct(
*/
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
if ($value instanceof ContentEntityInterface) {
$tags = $this->metatagManager->tagsFromEntityWithDefaults($value);
$elements = $this->metatagManager->generateRawElements($tags, $value);

foreach ($elements as $element) {
yield $element;
$tags = metatag_get_tags_from_route($value);
// Trigger hook_metatags_attachments_alter().
// Allow modules to rendered metatags prior to attaching.
\Drupal::service('module_handler')->alter('metatags_attachments', $tags);
$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_map('reset', $tags);
foreach ($tags as $tag) {
yield $tag;
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/Plugin/GraphQL/Fields/InternalUrl/Metatags.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ protected function resolveValues($value, array $args, ResolveContext $context, R
if ($value instanceof Url) {
$resolve = $this->subRequestBuffer->add($value, function () {
$tags = metatag_get_tags_from_route();
// Trigger hook_metatags_attachments_alter().
// Allow modules to rendered metatags prior to attaching.
\Drupal::service('module_handler')->alter('metatags_attachments', $tags);
$tags = NestedArray::getValue($tags, ['#attached', 'html_head']) ?: [];
$tags = array_filter($tags, function($tag) {
$tags = array_filter($tags, function ($tag) {
return is_array($tag) && in_array(NestedArray::getValue($tag, [0, '#tag']), ['meta', 'link']);
});

Expand Down