|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Language\LanguageInterface; |
|
6
|
|
|
use Drupal\Core\Language\LanguageManagerInterface; |
|
7
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
8
|
|
|
use Drupal\Core\Url; |
|
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
11
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @GraphQLField( |
|
15
|
|
|
* id = "url_language_switch_links", |
|
16
|
|
|
* secure = true, |
|
17
|
|
|
* name = "languageSwitchLinks", |
|
18
|
|
|
* multi = true, |
|
19
|
|
|
* type = "LanguageSwitchLink", |
|
20
|
|
|
* types = {"Url"} |
|
21
|
|
|
* ) |
|
22
|
|
|
*/ |
|
23
|
|
|
class LanguageSwitchLinks extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The language manager. |
|
27
|
|
|
* |
|
28
|
|
|
* @var \Drupal\Core\Language\LanguageManagerInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $languageManager; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
36
|
|
|
return new static($configuration, $plugin_id, $plugin_definition, $container->get('language_manager')); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition, LanguageManagerInterface $languageManager) { |
|
43
|
|
|
$this->languageManager = $languageManager; |
|
44
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function resolveValues($value, array $args, ResolveInfo $info) { |
|
51
|
|
|
if ($value instanceof Url) { |
|
|
|
|
|
|
52
|
|
|
$links = $this->languageManager->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, $value); |
|
53
|
|
|
if (!empty($links->links)) { |
|
54
|
|
|
$current_langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_URL)->getId(); |
|
55
|
|
|
foreach ($links->links as $link) { |
|
56
|
|
|
/** @var \Drupal\Core\Url $url */ |
|
57
|
|
|
$url = $link['url']; |
|
58
|
|
|
/** @var \Drupal\Core\Language\Language $language */ |
|
59
|
|
|
$language = $link['language']; |
|
60
|
|
|
$langcode = $language->getId(); |
|
61
|
|
|
|
|
62
|
|
|
yield [ |
|
63
|
|
|
'langcode' => $langcode, |
|
64
|
|
|
'url' => $url->setOption('language', $language), |
|
65
|
|
|
'title' => $link['title'], |
|
66
|
|
|
'isActive' => $langcode === $current_langcode, |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|