|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\Deriver\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Component\Plugin\Derivative\DeriverBase; |
|
6
|
|
|
use Drupal\Core\Entity\EntityFieldManagerInterface; |
|
7
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
8
|
|
|
use Drupal\Core\Entity\FieldableEntityInterface; |
|
9
|
|
|
use Drupal\Core\Field\BaseFieldDefinition; |
|
10
|
|
|
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; |
|
11
|
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait; |
|
12
|
|
|
use Drupal\Core\TypedData\TypedDataManagerInterface; |
|
13
|
|
|
use Drupal\graphql\Utility\StringHelper; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
15
|
|
|
|
|
16
|
|
|
class EntityReferenceQueryDeriver extends DeriverBase implements ContainerDeriverInterface { |
|
17
|
|
|
use StringTranslationTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The entity type manager. |
|
21
|
|
|
* |
|
22
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $entityTypeManager; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The entity field manager. |
|
28
|
|
|
* |
|
29
|
|
|
* @var \Drupal\Core\Entity\EntityFieldManagerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $entityFieldManager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The typed data manager service. |
|
35
|
|
|
* |
|
36
|
|
|
* @var \Drupal\Core\TypedData\TypedDataManagerInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $typedDataManager; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function create(ContainerInterface $container, $basePluginId) { |
|
44
|
|
|
return new static( |
|
45
|
|
|
$container->get('entity_type.manager'), |
|
46
|
|
|
$container->get('entity_field.manager'), |
|
47
|
|
|
$container->get('typed_data_manager') |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* RawValueFieldItemDeriver constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
|
55
|
|
|
* The entity type manager. |
|
56
|
|
|
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager |
|
57
|
|
|
* The entity field manager. |
|
58
|
|
|
* @param \Drupal\Core\TypedData\TypedDataManagerInterface $typedDataManager |
|
59
|
|
|
* The typed data manager service. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct( |
|
62
|
|
|
EntityTypeManagerInterface $entityTypeManager, |
|
63
|
|
|
EntityFieldManagerInterface $entityFieldManager, |
|
64
|
|
|
TypedDataManagerInterface $typedDataManager |
|
65
|
|
|
) { |
|
66
|
|
|
$this->entityTypeManager = $entityTypeManager; |
|
67
|
|
|
$this->entityFieldManager = $entityFieldManager; |
|
68
|
|
|
$this->typedDataManager = $typedDataManager; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getDerivativeDefinitions($basePluginDefinition) { |
|
75
|
|
|
foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) { |
|
76
|
|
|
$interfaces = class_implements($entityType->getClass()); |
|
77
|
|
|
if (!array_key_exists(FieldableEntityInterface::class, $interfaces)) { |
|
78
|
|
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
foreach ($this->entityFieldManager->getFieldStorageDefinitions($entityTypeId) as $fieldDefinition) { |
|
82
|
|
|
if ($fieldDefinition->getType() !== 'entity_reference' || !$targetTypeId = $fieldDefinition->getSetting('target_type')) { |
|
83
|
|
|
continue; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$tags = array_merge($fieldDefinition->getCacheTags(), ['entity_field_info']); |
|
87
|
|
|
$contexts = $fieldDefinition->getCacheContexts(); |
|
88
|
|
|
$maxAge = $fieldDefinition->getCacheMaxAge(); |
|
89
|
|
|
|
|
90
|
|
|
$targetType = $this->entityTypeManager->getDefinition($targetTypeId); |
|
91
|
|
|
$fieldName = $fieldDefinition->getName(); |
|
92
|
|
|
$derivative = [ |
|
93
|
|
|
'parents' => [StringHelper::camelCase($entityTypeId)], |
|
94
|
|
|
'name' => StringHelper::propCase('query', $fieldName), |
|
95
|
|
|
'description' => $this->t('Query reference: @description', [ |
|
96
|
|
|
'@description' => $fieldDefinition->getDescription(), |
|
97
|
|
|
]), |
|
98
|
|
|
'field' => $fieldName, |
|
99
|
|
|
'entity_key' => $targetType->getKey('id'), |
|
100
|
|
|
'entity_type' => $targetTypeId, |
|
101
|
|
|
'schema_cache_tags' => $tags, |
|
102
|
|
|
'schema_cache_contexts' => $contexts, |
|
103
|
|
|
'schema_cache_max_age' => $maxAge, |
|
104
|
|
|
'response_cache_tags' => $tags, |
|
105
|
|
|
'response_cache_contexts' => $contexts, |
|
106
|
|
|
'response_cache_max_age' => $maxAge, |
|
107
|
|
|
] + $basePluginDefinition; |
|
108
|
|
|
|
|
109
|
|
|
/** @var \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface $definition */ |
|
110
|
|
|
$this->derivatives["$entityTypeId-$fieldName"] = $derivative; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->derivatives; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|