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\Plugin\ContainerFactoryPluginInterface; |
8
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
9
|
|
|
use Drupal\system\MenuInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
11
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Retrieve a menu by it's name. |
15
|
|
|
* |
16
|
|
|
* @GraphQLField( |
17
|
|
|
* id = "menu_by_name", |
18
|
|
|
* secure = true, |
19
|
|
|
* name = "menuByName", |
20
|
|
|
* type = "Menu", |
21
|
|
|
* arguments = { |
22
|
|
|
* "name" = "String" |
23
|
|
|
* } |
24
|
|
|
* ) |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
class MenuByName extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
27
|
|
|
use DependencySerializationTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The entity type manager. |
31
|
|
|
* |
32
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
33
|
|
|
*/ |
34
|
|
|
protected $entityTypeManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
40
|
|
|
return new static($configuration, $pluginId, $pluginDefinition, $container->get('entity_type.manager')); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) { |
47
|
|
|
$this->entityTypeManager = $entityTypeManager; |
48
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function resolveValues($value, array $args, ResolveInfo $info) { |
55
|
|
|
$entity = $this->entityTypeManager->getStorage('menu')->load($args['name']); |
56
|
|
|
|
57
|
|
|
if ($entity instanceof MenuInterface) { |
|
|
|
|
58
|
|
|
yield $entity; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
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.