Skip to content
Snippets Groups Projects
Commit 930a09e8 authored by Luis's avatar Luis
Browse files

3217644 Fix bug on retrieve tags and routing

parent b2c8303c
No related branches found
No related tags found
No related merge requests found
......@@ -9,11 +9,17 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use Drupal\metatag\MetatagManagerInterface;
use GraphQL\Type\Definition\ResolveInfo;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
/**
* Graphql field to retrieve entity metatags.
*
* @GraphQLField(
* secure = true,
* id = "entity_metatags",
......@@ -28,18 +34,25 @@ class EntityMetatags extends FieldPluginBase implements ContainerFactoryPluginIn
use DependencySerializationTrait;
/**
* The metatag manager service.
* Module Handler.
*
* @var \Drupal\metatag\MetatagManagerInterface
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $metatagManager;
protected $moduleHandler;
/**
* Module Handler.
* Request context.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
* @var \Symfony\Component\Routing\RequestContext
*/
protected $moduleHandler;
protected $requestContext;
/**
* Request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* {@inheritdoc}
......@@ -49,8 +62,9 @@ class EntityMetatags extends FieldPluginBase implements ContainerFactoryPluginIn
$configuration,
$pluginId,
$pluginDefinition,
$container->get('metatag.manager'),
$container->get('module_handler')
$container->get('module_handler'),
$container->get('router.request_context'),
$container->get('request_stack'),
);
}
......@@ -63,37 +77,37 @@ class EntityMetatags extends FieldPluginBase implements ContainerFactoryPluginIn
* The plugin id.
* @param mixed $pluginDefinition
* The plugin definition array.
* @param \Drupal\metatag\MetatagManagerInterface $metatagManager
* Metatag Manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* Module Handler.
* @param \Symfony\Component\Routing\RequestContext $requestContext
* Request context.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* Request stack.
*/
public function __construct(
array $configuration,
string $pluginId,
$pluginDefinition,
MetatagManagerInterface $metatagManager,
ModuleHandlerInterface $moduleHandler
ModuleHandlerInterface $moduleHandler,
RequestContext $requestContext,
RequestStack $requestStack
) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->metatagManager = $metatagManager;
$this->moduleHandler = $moduleHandler;
$this->requestContext = $requestContext;
$this->requestStack = $requestStack;
}
/**
* {@inheritdoc}
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
if ($value instanceof ContentEntityInterface) {
$tags = $this->metatagManager->tagsFromEntityWithDefaults($value);
// Trigger hook_metatags_attachments_alter().
// Allow modules to rendered metatags prior to attaching.
$this->moduleHandler->alter('metatags_attachments', $tags);
// Filter non schema metatags, because schema metatags are processed in
// EntitySchemaMetatags class.
$elements = $this->metatagManager->generateRawElements($tags, $value);
$this->changeRouteContext($value);
$elements = metatag_generate_entity_metatags($value);
$this->revertRouteContext();
$elements = array_filter($elements, function ($metatag_object) {
return !NestedArray::getValue($metatag_object, [
'#attributes',
......@@ -107,4 +121,28 @@ class EntityMetatags extends FieldPluginBase implements ContainerFactoryPluginIn
}
}
/**
* Change context route.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
protected function changeRouteContext(ContentEntityInterface $entity): void {
$uri = $entity->toUrl()->toString();
$request = Request::create($uri);
$request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'entity.node.canonical');
$request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route($uri));
$this->requestStack->push($request);
$this->requestContext->fromRequest($request);
}
/**
* Revert to previous request.
*/
protected function revertRouteContext(): void {
$this->requestStack->pop();
}
}
......@@ -8,6 +8,8 @@ use Drupal\schema_metatag\SchemaMetatagManager;
use GraphQL\Type\Definition\ResolveInfo;
/**
* Graphql field to retrieve entity schema metatags.
*
* @GraphQLField(
* secure = true,
* id = "entity_schema_metatags",
......@@ -21,18 +23,16 @@ class EntitySchemaMetatags extends EntityMetatags {
/**
* {@inheritdoc}
*
* @throws \JsonException
*/
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
if ($value instanceof ContentEntityInterface && $this->isModuleSchemaActive()) {
$tags = $this->metatagManager->tagsFromEntityWithDefaults($value);
// Trigger hook_metatags_attachments_alter().
// Allow modules to rendered metatags prior to attaching.
$this->moduleHandler->alter('metatags_attachments', $tags);
$elements = $this->metatagManager->generateElements($tags, $value);
$this->changeRouteContext($value);
$elements = metatag_get_tags_from_route($value);
$this->revertRouteContext();
$jsonLd = SchemaMetatagManager::parseJsonld($elements['#attached']['html_head']);
yield (string) json_encode($jsonLd);
yield (string) json_encode($jsonLd, JSON_THROW_ON_ERROR);
}
else {
yield t('The module schema_metatag is not installed.');
......@@ -40,9 +40,10 @@ class EntitySchemaMetatags extends EntityMetatags {
}
/**
* Chec if module SCHEMA_METATAG_MODULE_NAME is active.
* Check if module SCHEMA_METATAG_MODULE_NAME is active.
*
* @return bool
* Is module schema active.
*/
private function isModuleSchemaActive(): bool {
return $this->moduleHandler->moduleExists(SCHEMA_METATAG_MODULE_NAME);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment