1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\LanguageSwitch; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface; |
6
|
|
|
use Drupal\Core\Language\LanguageInterface; |
7
|
|
|
use Drupal\Core\Language\LanguageManagerInterface; |
8
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
9
|
|
|
use Drupal\Core\Url; |
10
|
|
|
use Drupal\graphql\GraphQL\Buffers\SubRequestBuffer; |
11
|
|
|
use Drupal\graphql\GraphQL\Execution\ResolveContext; |
12
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @GraphQLField( |
18
|
|
|
* id = "url_language_switch_links", |
19
|
|
|
* secure = true, |
20
|
|
|
* name = "languageSwitchLinks", |
21
|
|
|
* type = "[LanguageSwitchLink]", |
22
|
|
|
* parents = {"InternalUrl"}, |
23
|
|
|
* arguments = { |
24
|
|
|
* "language" = "LanguageId" |
25
|
|
|
* }, |
26
|
|
|
* response_cache_contexts = { |
27
|
|
|
* "languages:language_url", |
28
|
|
|
* "languages:language_interface", |
29
|
|
|
* }, |
30
|
|
|
* contextual_arguments = {"language"} |
31
|
|
|
* ) |
32
|
|
|
*/ |
33
|
|
|
class LanguageSwitchLinks extends FieldPluginBase implements ContainerFactoryPluginInterface { |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The language manager. |
37
|
|
|
* |
38
|
|
|
* @var \Drupal\Core\Language\LanguageManagerInterface |
39
|
|
|
*/ |
40
|
|
|
protected $languageManager; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The subrequest buffer service. |
44
|
|
|
* |
45
|
|
|
* @var \Drupal\graphql\GraphQL\Buffers\SubRequestBuffer |
46
|
|
|
*/ |
47
|
|
|
protected $subRequestBuffer; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The config factory. |
51
|
|
|
* |
52
|
|
|
* @var \Drupal\Core\Config\ConfigFactoryInterface |
53
|
|
|
*/ |
54
|
|
|
protected $configFactory; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
60
|
|
|
return new static( |
61
|
|
|
$configuration, |
62
|
|
|
$plugin_id, |
63
|
|
|
$plugin_definition, |
64
|
|
|
$container->get('language_manager'), |
65
|
|
|
$container->get('graphql.buffer.subrequest'), |
66
|
|
|
$container->get('config.factory') |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function __construct( |
74
|
|
|
array $configuration, |
75
|
|
|
$pluginId, |
76
|
|
|
$pluginDefinition, |
77
|
|
|
LanguageManagerInterface $languageManager, |
78
|
|
|
SubRequestBuffer $subRequestBuffer, |
79
|
|
|
ConfigFactoryInterface $configFactory |
80
|
|
|
) { |
81
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
82
|
|
|
$this->languageManager = $languageManager; |
83
|
|
|
$this->subRequestBuffer = $subRequestBuffer; |
84
|
|
|
$this->configFactory = $configFactory; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
91
|
|
|
if ($value instanceof Url) { |
|
|
|
|
92
|
|
|
|
93
|
|
|
// Use the <front> route if the requested url is the frontpage. |
94
|
|
|
$frontpage = $this->configFactory->get('system.site')->get('page.front'); |
95
|
|
|
if ('/' . $value->getInternalPath() === $frontpage) { |
96
|
|
|
$value = Url::fromRoute('<front>'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$links = $this->languageManager->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, $value); |
100
|
|
|
|
101
|
|
|
$current = $this->languageManager->getLanguage($args['language']); |
102
|
|
|
if (!$current) { |
103
|
|
|
$current = $this->languageManager->getDefaultLanguage(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!empty($links->links)) { |
107
|
|
|
foreach ($links->links as $link) { |
108
|
|
|
// Yield the link array and the language object of the language |
109
|
|
|
// context resolved from the sub-request. |
110
|
|
|
yield [ |
111
|
|
|
'link' => $link, |
112
|
|
|
'context' => $current, |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.