1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Menu; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
6
|
|
|
use Drupal\Core\Entity\EntityType; |
7
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
8
|
|
|
use Drupal\Core\Entity\TranslatableInterface; |
9
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
10
|
|
|
use Drupal\graphql\GraphQL\Execution\ResolveContext; |
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
12
|
|
|
use Drupal\system\MenuInterface; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Retrieve a menu by it's name. |
18
|
|
|
* |
19
|
|
|
* @GraphQLField( |
20
|
|
|
* id = "menu_by_name", |
21
|
|
|
* secure = true, |
22
|
|
|
* name = "menuByName", |
23
|
|
|
* description = @Translation("Loads a menu by its machine-readable name."), |
24
|
|
|
* type = "Menu", |
25
|
|
|
* arguments = { |
26
|
|
|
* "name" = "String!", |
27
|
|
|
* "language" = "LanguageId" |
28
|
|
|
* }, |
29
|
|
|
* contextual_arguments = {"language"}, |
30
|
|
|
* response_cache_contexts = {"languages:language_interface"} |
31
|
|
|
* ) |
32
|
|
|
*/ |
33
|
|
|
class MenuByName extends FieldPluginBase implements ContainerFactoryPluginInterface { |
34
|
|
|
use DependencySerializationTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The entity type manager. |
38
|
|
|
* |
39
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
40
|
|
|
*/ |
41
|
|
|
protected $entityTypeManager; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
47
|
|
|
return new static($configuration, $pluginId, $pluginDefinition, $container->get('entity_type.manager')); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* MenuByName constructor. |
52
|
|
|
* |
53
|
|
|
* @param array $configuration |
54
|
|
|
* The plugin configuration array. |
55
|
|
|
* @param string $pluginId |
56
|
|
|
* The plugin id. |
57
|
|
|
* @param mixed $pluginDefinition |
58
|
|
|
* The plugin definition. |
59
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
60
|
|
|
* The entity type manager service. |
61
|
|
|
*/ |
62
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) { |
63
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
64
|
|
|
$this->entityTypeManager = $entityTypeManager; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
71
|
|
|
$entity = $this->entityTypeManager->getStorage('menu')->load($args['name']); |
72
|
|
|
|
73
|
|
|
if ($entity instanceof MenuInterface) { |
|
|
|
|
74
|
|
|
if (isset($args['language']) && $args['language'] != $entity->language()->getId() && $entity instanceof TranslatableInterface && $entity->isTranslatable()) { |
|
|
|
|
75
|
|
|
if ($entity->hasTranslation($args['language'])) { |
76
|
|
|
$entity = $entity->getTranslation($args['language']); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
yield $entity; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
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.