Skip to content

Commit 32ff4d3

Browse files
authored
Do not generate a separate field type per bundle. (#400)
1 parent 5078482 commit 32ff4d3

6 files changed

Lines changed: 69 additions & 118 deletions

File tree

modules/graphql_core/src/Plugin/Deriver/Fields/EntityFieldDeriverBase.php renamed to modules/graphql_core/src/Plugin/Deriver/EntityFieldDeriverBase.php

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<?php
22

3-
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
3+
namespace Drupal\graphql_core\Plugin\Deriver;
44

55
use Drupal\Component\Plugin\Derivative\DeriverBase;
66
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
77
use Drupal\Core\Entity\EntityTypeManagerInterface;
88
use Drupal\Core\Entity\FieldableEntityInterface;
9-
use Drupal\Core\Field\BaseFieldDefinition;
109
use Drupal\Core\Field\FieldStorageDefinitionInterface;
1110
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
1211
use Drupal\Core\Entity\EntityFieldManagerInterface;
@@ -18,30 +17,16 @@
1817
abstract class EntityFieldDeriverBase extends DeriverBase implements ContainerDeriverInterface {
1918

2019
/**
21-
* Provides plugin definition values from base fields.
20+
* Provides plugin definition values from fields.
2221
*
2322
* @param string $entityTypeId
2423
* The host entity type.
25-
* @param \Drupal\Core\Field\BaseFieldDefinition $baseFieldDefinition
26-
* Base field definition object.
24+
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $fieldDefinition
25+
* Field definition object.
2726
* @param array $basePluginDefinition
2827
* Base definition array.
2928
*/
30-
protected function getBaseFieldDefinition($entityTypeId, BaseFieldDefinition $baseFieldDefinition, array $basePluginDefinition) {}
31-
32-
/**
33-
* Provides plugin definition values from config field storage.
34-
*
35-
* @param string $entityTypeId
36-
* The host entity type.
37-
* @param string $bundleId
38-
* The host entity bundle.
39-
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage
40-
* Field storage definition object.
41-
* @param array $basePluginDefinition
42-
* Base definition array.
43-
*/
44-
protected function getConfigFieldDefinition($entityTypeId, $bundleId, FieldStorageDefinitionInterface $storage, array $basePluginDefinition) {}
29+
abstract protected function getDerivativeDefinitionsFromFieldDefinition($entityTypeId, FieldStorageDefinitionInterface $fieldDefinition, array $basePluginDefinition);
4530

4631
/**
4732
* The entity type manager.
@@ -113,26 +98,14 @@ public function __construct(
11398
public function getDerivativeDefinitions($basePluginDefinition) {
11499
$this->derivatives = [];
115100

116-
/** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundleInfo */
117-
$bundleInfo = \Drupal::service('entity_type.bundle.info');
118-
119101
foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
120102
$interfaces = class_implements($entityType->getClass());
121103
if (!array_key_exists(FieldableEntityInterface::class, $interfaces)) {
122104
continue;
123105
}
124106

125-
foreach ($this->entityFieldManager->getBaseFieldDefinitions($entityTypeId) as $baseFieldDefinition) {
126-
$this->getBaseFieldDefinition($entityTypeId, $baseFieldDefinition, $basePluginDefinition);
127-
}
128-
129-
foreach ($bundleInfo->getBundleInfo($entityTypeId) as $bundleId => $bundle) {
130-
foreach ($this->entityFieldManager->getFieldDefinitions($entityTypeId, $bundleId) as $fieldDefinition) {
131-
$storage = $fieldDefinition->getFieldStorageDefinition();
132-
if (!$storage->isBaseField()) {
133-
$this->getConfigFieldDefinition($entityTypeId, $bundleId, $storage, $basePluginDefinition);
134-
}
135-
}
107+
foreach ($this->entityFieldManager->getFieldStorageDefinitions($entityTypeId) as $fieldStorageDefinition) {
108+
$this->derivatives = array_merge($this->derivatives, $this->getDerivativeDefinitionsFromFieldDefinition($entityTypeId, $fieldStorageDefinition, $basePluginDefinition));
136109
}
137110
}
138111

modules/graphql_core/src/Plugin/Deriver/EntityFieldDeriverWithTypeMapping.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,11 @@
55
use Drupal\Core\Entity\EntityFieldManagerInterface;
66
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
77
use Drupal\Core\Entity\EntityTypeManagerInterface;
8-
use Drupal\Core\Field\BaseFieldDefinition;
9-
use Drupal\Core\Field\FieldStorageDefinitionInterface;
10-
use Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldDeriverBase;
118
use Drupal\graphql_core\TypeMapper;
129
use Symfony\Component\DependencyInjection\ContainerInterface;
1310

1411
abstract class EntityFieldDeriverWithTypeMapping extends EntityFieldDeriverBase {
1512

16-
/**
17-
* Provide plugin definition values from both base and config fields.
18-
*
19-
* @param string $entityTypeId
20-
* The host entity type.
21-
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
22-
* Field definition object.
23-
* @param array $basePluginDefinition
24-
* Base plugin definition array.
25-
* @param null|string $bundleId
26-
* Bundle id.
27-
*/
28-
abstract protected function getDerivativesFromPropertyDefinitions($entityTypeId, FieldStorageDefinitionInterface $definition, array $basePluginDefinition, $bundleId = NULL);
29-
3013
/**
3114
* The type mapper service.
3215
*
@@ -72,18 +55,4 @@ public function __construct(
7255
$this->typeMapper = $typeMapper;
7356
}
7457

75-
/**
76-
* {@inheritdoc}
77-
*/
78-
protected function getBaseFieldDefinition($entityTypeId, BaseFieldDefinition $baseFieldDefinition, array $basePluginDefinition) {
79-
$this->getDerivativesFromPropertyDefinitions($entityTypeId, $baseFieldDefinition, $basePluginDefinition);
80-
}
81-
82-
/**
83-
* {@inheritdoc}
84-
*/
85-
protected function getConfigFieldDefinition($entityTypeId, $bundleId, FieldStorageDefinitionInterface $storage, array $basePluginDefinition) {
86-
$this->getDerivativesFromPropertyDefinitions($entityTypeId, $storage, $basePluginDefinition, $bundleId);
87-
}
88-
8958
}

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

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

5+
use Drupal\Core\Field\FieldConfigInterface;
56
use Drupal\Core\Field\FieldStorageDefinitionInterface;
7+
use Drupal\field\FieldStorageConfigInterface;
68
use Drupal\graphql\Utility\StringHelper;
79
use Drupal\graphql_core\Plugin\Deriver\EntityFieldDeriverWithTypeMapping;
810
use Drupal\graphql_core\Plugin\GraphQL\Fields\Entity\EntityField;
@@ -22,20 +24,23 @@ class EntityFieldDeriver extends EntityFieldDeriverWithTypeMapping {
2224
/**
2325
* {@inheritdoc}
2426
*/
25-
protected function getDerivativesFromPropertyDefinitions($entityTypeId, FieldStorageDefinitionInterface $definition, array $basePluginDefinition, $bundleId = NULL) {
26-
$fieldName = $definition->getName();
27+
protected function getDerivativeDefinitionsFromFieldDefinition($entityTypeId, FieldStorageDefinitionInterface $fieldDefinition, array $basePluginDefinition) {
28+
$fieldName = $fieldDefinition->getName();
29+
if (!$parents = $this->getParentsForField($entityTypeId, $fieldDefinition)) {
30+
return [];
31+
}
2732

2833
$derivative = [
29-
'parents' => isset($bundleId) ? [StringHelper::camelCase([$entityTypeId, $bundleId])] : [StringHelper::camelCase([$entityTypeId])],
34+
'parents' => $parents,
3035
'name' => EntityField::getId($fieldName),
31-
'multi' => $definition->isMultiple(),
36+
'multi' => $fieldDefinition->isMultiple(),
3237
'field' => $fieldName,
33-
'schema_cache_tags' => array_merge($definition->getCacheTags(), ['entity_field_info']),
34-
'schema_cache_contexts' => $definition->getCacheContexts(),
35-
'schema_cache_max_age' => $definition->getCacheMaxAge(),
38+
'schema_cache_tags' => array_merge($fieldDefinition->getCacheTags(), ['entity_field_info']),
39+
'schema_cache_contexts' => $fieldDefinition->getCacheContexts(),
40+
'schema_cache_max_age' => $fieldDefinition->getCacheMaxAge(),
3641
];
3742

38-
$properties = $definition->getPropertyDefinitions();
43+
$properties = $fieldDefinition->getPropertyDefinitions();
3944
if (count($properties) === 1) {
4045
// Flatten the structure for single-property fields.
4146
/** @var \Drupal\Core\TypedData\DataDefinitionInterface $property */
@@ -49,7 +54,27 @@ protected function getDerivativesFromPropertyDefinitions($entityTypeId, FieldSto
4954
$derivative['type'] = EntityFieldType::getId($entityTypeId, $fieldName);
5055
}
5156

52-
$key = !isset($bundleId) ? "$entityTypeId-$fieldName" : "$entityTypeId-$bundleId-$fieldName";
53-
$this->derivatives[$key] = $derivative + $basePluginDefinition;
57+
return [
58+
"$entityTypeId-$fieldName" => $derivative + $basePluginDefinition,
59+
];
60+
}
61+
62+
/**
63+
* @param $entityTypeId
64+
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $fieldDefinition
65+
* @return array
66+
*/
67+
protected function getParentsForField($entityTypeId, FieldStorageDefinitionInterface $fieldDefinition) {
68+
if ($fieldDefinition->isBaseField()) {
69+
return [StringHelper::camelCase([$entityTypeId])];
70+
}
71+
72+
if ($fieldDefinition instanceof FieldStorageConfigInterface) {
73+
return array_values(array_map(function ($bundleId) use ($entityTypeId) {
74+
return StringHelper::camelCase([$entityTypeId, $bundleId]);
75+
}, $fieldDefinition->getBundles()));
76+
}
77+
78+
return [];
5479
}
5580
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,31 @@ class EntityFieldItemDeriver extends EntityFieldDeriverWithTypeMapping {
1212
/**
1313
* {@inheritdoc}
1414
*/
15-
protected function getDerivativesFromPropertyDefinitions($entityTypeId, FieldStorageDefinitionInterface $definition, array $basePluginDefinition, $bundleId = NULL) {
16-
$fieldName = $definition->getName();
17-
$dataType = EntityFieldType::getId($entityTypeId, $fieldName);
15+
protected function getDerivativeDefinitionsFromFieldDefinition($entityTypeId, FieldStorageDefinitionInterface $fieldDefinition, array $basePluginDefinition, $bundleId = NULL) {
16+
$derivatives = [];
17+
$fieldName = $fieldDefinition->getName();
18+
$commonDefinition = [
19+
'parents' => [EntityFieldType::getId($entityTypeId, $fieldName)],
20+
'schema_cache_tags' => array_merge($fieldDefinition->getCacheTags(), ['entity_field_info']),
21+
'schema_cache_contexts' => $fieldDefinition->getCacheContexts(),
22+
'schema_cache_max_age' => $fieldDefinition->getCacheMaxAge(),
23+
];
1824

19-
$propertyDefinitions = $definition->getPropertyDefinitions();
20-
foreach ($propertyDefinitions as $property => $propertyDefinition) {
25+
foreach ($fieldDefinition->getPropertyDefinitions() as $property => $propertyDefinition) {
2126
if ($propertyDefinition->getDataType() == 'map') {
2227
// TODO Is it possible to get the keys of a map (eg. the options array for link field) here?
2328
continue;
2429
}
2530

26-
$this->derivatives["$entityTypeId-$fieldName-$property"] = [
31+
$derivatives["$entityTypeId-$fieldName-$property"] = [
2732
'name' => StringHelper::propCase($property),
2833
'property' => $property,
2934
'multi' => FALSE,
3035
'type' => $this->typeMapper->typedDataToGraphQLFieldType($propertyDefinition),
31-
'parents' => [$dataType],
32-
'schema_cache_tags' => array_merge($definition->getCacheTags(), ['entity_field_info']),
33-
'schema_cache_contexts' => $definition->getCacheContexts(),
34-
'schema_cache_max_age' => $definition->getCacheMaxAge(),
35-
] + $basePluginDefinition;
36+
] + $commonDefinition + $basePluginDefinition;
3637
}
38+
39+
return $derivatives;
3740
}
3841

3942
}

modules/graphql_core/src/Plugin/Deriver/Types/EntityFieldTypeDeriver.php

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
namespace Drupal\graphql_core\Plugin\Deriver\Types;
44

55
use Drupal\Core\Field\FieldStorageDefinitionInterface;
6-
use Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldDeriverBase;
7-
use Drupal\Core\Field\BaseFieldDefinition;
8-
use Drupal\graphql_core\Plugin\GraphQL\Interfaces\Entity\EntityType;
6+
use Drupal\graphql_core\Plugin\Deriver\EntityFieldDeriverBase;
97
use Drupal\graphql_core\Plugin\GraphQL\Types\Entity\EntityFieldType;
10-
use Drupal\graphql_core\Plugin\GraphQL\Types\Entity\EntityBundle;
118

129
/**
1310
* Derive GraphQL types for raw values of drupal fields.
@@ -17,36 +14,20 @@ class EntityFieldTypeDeriver extends EntityFieldDeriverBase {
1714
/**
1815
* {@inheritdoc}
1916
*/
20-
protected function getBaseFieldDefinition($entityTypeId, BaseFieldDefinition $baseFieldDefinition, array $basePluginDefinition) {
21-
$fieldName = $baseFieldDefinition->getName();
22-
23-
if ($this->isSinglePropertyField($baseFieldDefinition)) {
24-
return;
17+
protected function getDerivativeDefinitionsFromFieldDefinition($entityTypeId, FieldStorageDefinitionInterface $fieldDefinition, array $basePluginDefinition) {
18+
if ($this->isSinglePropertyField($fieldDefinition)) {
19+
return [];
2520
}
2621

27-
$this->derivatives["$entityTypeId-$fieldName"] = [
28-
'name' => EntityFieldType::getId($entityTypeId, $fieldName),
29-
'entity_type' => $entityTypeId,
30-
'data_type' => "entity:$entityTypeId:$fieldName",
31-
] + $basePluginDefinition;
32-
}
33-
34-
/**
35-
* {@inheritdoc}
36-
*/
37-
protected function getConfigFieldDefinition($entityTypeId, $bundleId, FieldStorageDefinitionInterface $storage, array $basePluginDefinition) {
38-
$fieldName = $storage->getName();
39-
40-
if ($this->isSinglePropertyField($storage)) {
41-
return;
42-
}
22+
$fieldName = $fieldDefinition->getName();
4323

44-
$this->derivatives["$entityTypeId-$bundleId-$fieldName"] = [
45-
'name' => EntityFieldType::getId($entityTypeId, $fieldName),
46-
'entity_type' => $entityTypeId,
47-
'data_type' => "entity:$entityTypeId:$bundleId:$fieldName",
48-
'bundle' => $bundleId,
49-
] + $basePluginDefinition;
24+
return [
25+
"$entityTypeId-$fieldName" => [
26+
'name' => EntityFieldType::getId($entityTypeId, $fieldName),
27+
'entity_type' => $entityTypeId,
28+
'field_name' => $fieldName,
29+
] + $basePluginDefinition,
30+
];
5031
}
5132

5233
}

modules/graphql_core/src/Plugin/GraphQL/Types/Entity/EntityFieldType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class EntityFieldType extends TypePluginBase {
2929
* The GraphQL type name.
3030
*/
3131
public static function getId($entityTypeId, $fieldName) {
32-
$result = StringHelper::camelCase(['type', $entityTypeId, $fieldName]);
32+
$result = StringHelper::camelCase(['field', $entityTypeId, $fieldName]);
3333
return $result;
3434
}
3535

0 commit comments

Comments
 (0)