Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
graphql_compose
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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
graphql_compose
Merge requests
!61
An error occurred while fetching the assigned milestone of the selected merge_request.
Support menu_link_url_translated
Code
Review changes
Check out branch
Open in Workspace
Download
Patches
Plain diff
Expand sidebar
Merged
Support menu_link_url_translated
issue/graphql_compose-3413606:3413606-support-for-translatable
into
2.1.x
Overview
0
Commits
3
Pipelines
4
Changes
2
Merged
Support menu_link_url_translated
Al Munnings
requested to merge
issue/graphql_compose-3413606:3413606-support-for-translatable
into
2.1.x
Jan 27, 2024
Overview
0
Commits
3
Pipelines
4
Changes
2
Closes
#3413606
0
0
Merge request reports
Compare
2.1.x
version 2
11520c7d
Jan 27, 2024
version 1
f24184fd
Jan 27, 2024
2.1.x (base)
and
latest version
latest version
74b6491b
3 commits,
Jan 27, 2024
version 2
11520c7d
2 commits,
Jan 27, 2024
version 1
f24184fd
1 commit,
Jan 27, 2024
2 files
+
118
−
1
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
modules/graphql_compose_menus/src/Plugin/GraphQL/DataProducer/MenuLinkUrlTranslated.php
0 → 100644
+
117
−
0
View file @ 74b6491b
Edit in single-file editor
Open in Web IDE
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\graphql_compose_menus\Plugin\GraphQL\DataProducer
;
use
Drupal\Core\Entity\EntityRepositoryInterface
;
use
Drupal\Core\Extension\ModuleHandlerInterface
;
use
Drupal\Core\Menu\MenuLinkInterface
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\Core\Url
;
use
Drupal\graphql\GraphQL\Buffers\EntityUuidBuffer
;
use
Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase
;
use
GraphQL\Deferred
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Returns the translated URL object of a menu link.
*
* @DataProducer(
* id = "menu_link_url_translated",
* name = @Translation("Menu link translated url"),
* description = @Translation("Returns the translated URL of a menu link."),
* produces = @ContextDefinition("any",
* label = @Translation("URL"),
* ),
* consumes = {
* "link" = @ContextDefinition("any",
* label = @Translation("Menu link"),
* ),
* },
* )
*/
class
MenuLinkUrlTranslated
extends
DataProducerPluginBase
implements
ContainerFactoryPluginInterface
{
/**
* Create the menu link translated URL resolver.
*
* @param array $configuration
* The plugin configuration.
* @param string $plugin_id
* The plugin id.
* @param mixed $plugin_definition
* The plugin definition.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
* The entity repository service.
* @param \Drupal\graphql\GraphQL\Buffers\EntityUuidBuffer $entityBuffer
* The entity buffer service.
*/
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
,
protected
ModuleHandlerInterface
$moduleHandler
,
protected
EntityRepositoryInterface
$entityRepository
,
protected
EntityUuidBuffer
$entityBuffer
,
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
);
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
return
new
static
(
$configuration
,
$plugin_id
,
$plugin_definition
,
$container
->
get
(
'module_handler'
),
$container
->
get
(
'entity.repository'
),
$container
->
get
(
'graphql.buffer.entity_uuid'
),
);
}
/**
* Resolve the translated menu link url.
*
* @param \Drupal\Core\Menu\MenuLinkInterface $link
* The menu link to resolve the url off of.
*
* @return \GraphQL\Deferred|\Drupal\Core\Url
* The Url or the deferred Url.
*/
public
function
resolve
(
MenuLinkInterface
$link
):
Deferred
|
Url
{
if
(
!
$this
->
moduleHandler
->
moduleExists
(
'translatable_menu_link_uri'
))
{
return
$link
->
getUrlObject
();
}
$plugin_id
=
$link
->
getBaseId
();
$derivative_id
=
$link
->
getDerivativeId
();
if
(
$plugin_id
!==
'menu_link_content'
||
empty
(
$derivative_id
))
{
return
$link
->
getUrlObject
();
}
$resolver
=
$this
->
entityBuffer
->
add
(
'menu_link_content'
,
$derivative_id
);
return
new
Deferred
(
function
()
use
(
$link
,
$resolver
)
{
if
(
$entity
=
$resolver
())
{
$translated_entity
=
$this
->
entityRepository
->
getTranslationFromContext
(
$entity
);
/** @var \Drupal\link\Plugin\Field\FieldType\LinkItem $link_item|null */
$link_item
=
$translated_entity
->
link_override
->
first
();
if
(
!
$link_item
->
isEmpty
())
{
return
$link_item
->
getUrl
();
}
}
return
$link
->
getUrlObject
();
});
}
}
Loading