1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
6
|
|
|
use Drupal\Core\Entity\EntityInterface; |
7
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
8
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
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 = "entity_bundle", |
16
|
|
|
* secure = true, |
17
|
|
|
* name = "entityBundle", |
18
|
|
|
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityBundleDeriver" |
19
|
|
|
* ) |
20
|
|
|
*/ |
21
|
|
|
class EntityBundle extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
22
|
|
|
use DependencySerializationTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The entity type manager. |
26
|
|
|
* |
27
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
28
|
|
|
*/ |
29
|
|
|
protected $entityTypeManager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* EntityBundle constructor. |
33
|
|
|
* |
34
|
|
|
* @param array $configuration |
35
|
|
|
* The plugin configuration array. |
36
|
|
|
* @param string $pluginId |
37
|
|
|
* The plugin id. |
38
|
|
|
* @param array $pluginDefinition |
39
|
|
|
* The plugin definition array. |
40
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
41
|
|
|
* The entity type manager service. |
42
|
|
|
*/ |
43
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) { |
44
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
45
|
|
|
$this->entityTypeManager = $entityTypeManager; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
52
|
|
|
return new static( |
53
|
|
|
$configuration, |
54
|
|
|
$pluginId, |
55
|
|
|
$pluginDefinition, |
56
|
|
|
$container->get('entity_type.manager') |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function resolveValues($value, array $args, ResolveInfo $info) { |
64
|
|
|
if ($value instanceof EntityInterface) { |
|
|
|
|
65
|
|
|
$bundle = $value->bundle(); |
66
|
|
|
|
67
|
|
|
if (($bundleEntityType = $value->getEntityType()->getBundleEntityType()) === NULL) { |
68
|
|
|
yield $bundle; |
69
|
|
|
} |
70
|
|
|
else { |
71
|
|
|
$bundleStorage = $this->entityTypeManager->getStorage($bundleEntityType); |
72
|
|
|
yield $bundleStorage->load($bundle); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|