Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
graphql_metatag
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
graphql_metatag
Commits
930a09e8
Commit
930a09e8
authored
4 years ago
by
Luis
Browse files
Options
Downloads
Patches
Plain Diff
3217644 Fix bug on retrieve tags and routing
parent
b2c8303c
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Plugin/GraphQL/Fields/Entity/EntityMetatags.php
+61
-23
61 additions, 23 deletions
src/Plugin/GraphQL/Fields/Entity/EntityMetatags.php
src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php
+10
-9
10 additions, 9 deletions
src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php
with
71 additions
and
32 deletions
src/Plugin/GraphQL/Fields/Entity/EntityMetatags.php
+
61
−
23
View file @
930a09e8
...
...
@@ -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\MetatagManag
erInterface
* @var \Drupal\
Core\Extension\ModuleHandl
erInterface
*/
protected
$m
etatagManag
er
;
protected
$m
oduleHandl
er
;
/**
*
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
();
}
}
This diff is collapsed.
Click to expand it.
src/Plugin/GraphQL/Fields/Entity/EntitySchemaMetatags.php
+
10
−
9
View file @
930a09e8
...
...
@@ -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.
* Chec
k
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
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment