Skip to content

Commit 1a194db

Browse files
authored
Fixing entity field derivers. (#392)
1 parent 52d81f8 commit 1a194db

3 files changed

Lines changed: 17 additions & 9 deletions

File tree

modules/graphql_content/src/Plugin/Deriver/DisplayedFieldDeriver.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Drupal\Core\Entity\EntityFieldManagerInterface;
99
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
1010
use Drupal\Core\Entity\EntityTypeManagerInterface;
11+
use Drupal\Core\Field\BaseFieldDefinition;
1112
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
1213
use Drupal\graphql\Utility\StringHelper;
1314
use Drupal\graphql_content\ContentEntitySchemaConfig;
@@ -62,6 +63,8 @@ class DisplayedFieldDeriver extends DeriverBase implements ContainerDeriverInter
6263
* Bundle info provider.
6364
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
6465
* Entity field manager instance.
66+
* @param \Drupal\graphql_content\ContentEntitySchemaConfig $schemaConfig
67+
* The schema config service.
6568
*/
6669
public function __construct(
6770
EntityTypeManagerInterface $entityTypeManager,
@@ -125,25 +128,31 @@ public function getDerivativeDefinitions($basePluginDefinition) {
125128
continue;
126129
}
127130

128-
$storages = $this->entityFieldManager->getFieldStorageDefinitions($typeId);
129-
131+
$storageDefinitions = $this->entityFieldManager->getFieldStorageDefinitions($typeId);
130132
foreach (array_keys($bundles[$typeId]) as $bundle) {
131133
if ($viewMode = $this->schemaConfig->getExposedViewMode($typeId, $bundle)) {
132134
if ($display = $this->getDisplay($typeId, $bundle, $viewMode)) {
133135
foreach ($display->getComponents() as $field => $component) {
134-
if (isset($component['type']) && $component['type'] == 'graphql_raw_value') {
136+
if (isset($component['type']) && $component['type'] === 'graphql_raw_value') {
135137
// Raw values formatter is obsolete.
136138
continue;
137139
}
138140

141+
$storageDefinition = isset($storageDefinitions[$field]) ? $storageDefinitions[$field] : NULL;
142+
if (isset($storageDefinition) && $storageDefinition instanceof BaseFieldDefinition) {
143+
// Skip base fields. We generate them globally across all
144+
// bundles.
145+
continue;
146+
}
147+
139148
$this->derivatives[$typeId . '-' . $bundle . '-' . $field] = [
140149
'name' => StringHelper::propCase($field),
141150
'types' => [StringHelper::camelCase([$typeId, $bundle])],
142151
'entity_type' => $typeId,
143152
'bundle' => $bundle,
144153
'field' => $field,
145-
'virtual' => !array_key_exists($field, $storages),
146-
'multi' => array_key_exists($field, $storages) ? $storages[$field]->getCardinality() != 1 : FALSE,
154+
'virtual' => !isset($storageDefinition),
155+
'multi' => isset($storageDefinition) ? $storageDefinition->getCardinality() != 1 : FALSE,
147156
'cache_tags' => $display->getCacheTags(),
148157
'cache_contexts' => $display->getCacheContexts(),
149158
'cache_max_age' => $display->getCacheMaxAge(),

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected function getDerivativesFromPropertyDefinitions($entityTypeId, FieldSto
2626
$fieldName = $definition->getName();
2727

2828
$derivative = [
29-
'types' => [StringHelper::camelCase([$entityTypeId])],
29+
'types' => isset($bundleId) ? [StringHelper::camelCase([$entityTypeId, $bundleId])] : [StringHelper::camelCase([$entityTypeId])],
3030
'name' => EntityField::getId($fieldName),
3131
'multi' => $definition->isMultiple(),
3232
'field' => $fieldName,
@@ -46,8 +46,7 @@ protected function getDerivativesFromPropertyDefinitions($entityTypeId, FieldSto
4646
$derivative['type'] = EntityFieldType::getId($entityTypeId, $fieldName);
4747
}
4848

49-
$key = is_null($bundleId) ? "$entityTypeId-$fieldName" : "$entityTypeId-$bundleId-$fieldName";
50-
49+
$key = !isset($bundleId) ? "$entityTypeId-$fieldName" : "$entityTypeId-$bundleId-$fieldName";
5150
$this->derivatives[$key] = $derivative + $basePluginDefinition;
5251
}
5352
}

modules/graphql_core/src/Plugin/GraphQL/Fields/Entity/EntityField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class EntityField extends EntityFieldBase {
2424

2525
/**
26-
* Returns a string if for the plugin.
26+
* Returns a string id for the plugin.
2727
*
2828
* @param string $fieldName
2929
* Field id.

0 commit comments

Comments
 (0)