Skip to content
Snippets Groups Projects

Support menu_link_url_translated

2 files
+ 118
1
Compare changes
  • Side-by-side
  • Inline
Files
2
<?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