Skip to content

Commit 9e14ee2

Browse files
committed
Simplified EntityFieldPropertyDeriver.
1 parent bd31691 commit 9e14ee2

1 file changed

Lines changed: 74 additions & 14 deletions

File tree

modules/graphql_core/src/Plugin/Deriver/Fields/EntityFieldPropertyDeriver.php

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,91 @@
22

33
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
44

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;
59
use Drupal\Core\Field\FieldStorageDefinitionInterface;
6-
use Drupal\graphql_core\Plugin\Deriver\EntityFieldDeriverWithTypeMapping;
10+
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
711
use Drupal\graphql_core\Plugin\GraphQL\Types\Entity\EntityFieldType;
12+
use Symfony\Component\DependencyInjection\ContainerInterface;
813

914
/**
1015
* Attach new properties to field types.
1116
*/
12-
class EntityFieldPropertyDeriver extends EntityFieldDeriverWithTypeMapping {
17+
class EntityFieldPropertyDeriver extends DeriverBase implements ContainerDeriverInterface {
1318

14-
protected function getDerivativeDefinitionsFromFieldDefinition(
15-
$entityTypeId,
16-
FieldStorageDefinitionInterface $fieldDefinition,
17-
array $basePluginDefinition
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+
* {@inheritdoc}
35+
*/
36+
public static function create(ContainerInterface $container, $basePluginId) {
37+
return new static(
38+
$container->get('entity_type.manager'),
39+
$container->get('entity_field.manager')
40+
);
41+
}
42+
43+
/**
44+
* RawValueFieldItemDeriver constructor.
45+
*
46+
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
47+
* The entity type manager.
48+
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
49+
* The entity field manager.
50+
*/
51+
public function __construct(
52+
EntityTypeManagerInterface $entityTypeManager,
53+
EntityFieldManagerInterface $entityFieldManager
1854
) {
55+
$this->entityTypeManager = $entityTypeManager;
56+
$this->entityFieldManager = $entityFieldManager;
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function getDerivativeDefinitions($basePluginDefinition) {
63+
$this->derivatives = [];
1964

20-
$derivatives = [];
65+
$parents = [];
2166

22-
if (isset($basePluginDefinition['field_types']) && in_array($fieldDefinition->getType(), $basePluginDefinition['field_types'])) {
23-
$fieldName = $fieldDefinition->getName();
24-
$derivatives["$entityTypeId-$fieldName-" . $basePluginDefinition['id']] = [
25-
'parents' => [EntityFieldType::getId($entityTypeId, $fieldName)]
26-
] + $basePluginDefinition;
67+
foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
68+
$interfaces = class_implements($entityType->getClass());
69+
if (!array_key_exists(FieldableEntityInterface::class, $interfaces)) {
70+
continue;
71+
}
72+
73+
$fieldDefinitions = $this->entityFieldManager
74+
->getFieldStorageDefinitions($entityTypeId);
75+
76+
foreach ($fieldDefinitions as $fieldDefinition) {
77+
$fieldName = $fieldDefinition->getName();
78+
$fieldType = $fieldDefinition->getType();
79+
80+
if (isset($basePluginDefinition['field_types']) && in_array($fieldType, $basePluginDefinition['field_types'])) {
81+
$parents[] = EntityFieldType::getId($entityTypeId, $fieldName);
82+
}
83+
}
2784
}
2885

29-
return $derivatives;
30-
}
86+
$this->derivatives[$basePluginDefinition['id']] = [
87+
'parents' => $parents,
88+
] + $basePluginDefinition;
3189

90+
return $this->derivatives;
91+
}
3292
}

0 commit comments

Comments
 (0)