1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Menu; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
6
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
7
|
|
|
use Drupal\Core\Language\LanguageManagerInterface; |
8
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
9
|
|
|
use Drupal\graphql\GraphQL\Execution\ResolveContext; |
10
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
11
|
|
|
use Drupal\system\MenuInterface; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
13
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Retrieve a menu by it's name. |
17
|
|
|
* |
18
|
|
|
* @GraphQLField( |
19
|
|
|
* id = "menu_by_name", |
20
|
|
|
* secure = true, |
21
|
|
|
* name = "menuByName", |
22
|
|
|
* description = @Translation("Loads a menu by its machine-readable name."), |
23
|
|
|
* type = "Menu", |
24
|
|
|
* arguments = { |
25
|
|
|
* "name" = "String!", |
26
|
|
|
* "language" = "LanguageId" |
27
|
|
|
* }, |
28
|
|
|
* contextual_arguments = {"language"}, |
29
|
|
|
* response_cache_contexts = {"languages:language_interface"} |
30
|
|
|
* ) |
31
|
|
|
*/ |
32
|
|
View Code Duplication |
class MenuByName extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
33
|
|
|
use DependencySerializationTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The entity type manager. |
37
|
|
|
* |
38
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
39
|
|
|
*/ |
40
|
|
|
protected $entityTypeManager; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
46
|
|
|
return new static($configuration, $pluginId, $pluginDefinition, $container->get('entity_type.manager')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* MenuByName constructor. |
51
|
|
|
* |
52
|
|
|
* @param array $configuration |
53
|
|
|
* The plugin configuration array. |
54
|
|
|
* @param string $pluginId |
55
|
|
|
* The plugin id. |
56
|
|
|
* @param mixed $pluginDefinition |
57
|
|
|
* The plugin definition. |
58
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
59
|
|
|
* The entity type manager service. |
60
|
|
|
*/ |
61
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) { |
62
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
63
|
|
|
$this->entityTypeManager = $entityTypeManager; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
70
|
|
|
$entity = $this->entityTypeManager->getStorage('menu')->load($args['name']); |
71
|
|
|
|
72
|
|
|
if ($entity instanceof MenuInterface) { |
|
|
|
|
73
|
|
|
yield $entity; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.