LanguageSwitchLinks.php in GraphQL 8.3
File
modules/graphql_core/src/Plugin/GraphQL/Fields/LanguageSwitch/LanguageSwitchLinks.php
View source
<?php
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\LanguageSwitch;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\graphql\GraphQL\Buffers\SubRequestBuffer;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use GraphQL\Type\Definition\ResolveInfo;
class LanguageSwitchLinks extends FieldPluginBase implements ContainerFactoryPluginInterface {
protected $languageManager;
protected $subRequestBuffer;
protected $configFactory;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('language_manager'), $container
->get('graphql.buffer.subrequest'), $container
->get('config.factory'));
}
public function __construct(array $configuration, $pluginId, $pluginDefinition, LanguageManagerInterface $languageManager, SubRequestBuffer $subRequestBuffer, ConfigFactoryInterface $configFactory) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->languageManager = $languageManager;
$this->subRequestBuffer = $subRequestBuffer;
$this->configFactory = $configFactory;
}
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
if ($value instanceof Url) {
$frontpage = $this->configFactory
->get('system.site')
->get('page.front');
if ('/' . $value
->getInternalPath() === $frontpage) {
$value = Url::fromRoute('<front>');
}
$links = $this->languageManager
->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, $value);
$current = $this->languageManager
->getLanguage($args['language']);
if (!$current) {
$current = $this->languageManager
->getDefaultLanguage();
}
if (!empty($links->links)) {
foreach ($links->links as $link) {
(yield [
'link' => $link,
'context' => $current,
]);
}
}
}
}
}
Classes
Name |
Description |
LanguageSwitchLinks |
Plugin annotation
@GraphQLField(
id = "url_language_switch_links",
secure = true,
name = "languageSwitchLinks",
type = "[LanguageSwitchLink]",
parents = {"InternalUrl"},
arguments = {
"language" = "LanguageId"
},
… |