Skip to content

Commit 3c8b237

Browse files
defrpmelab
authored andcommitted
Entity reference: Scope direct queries (#703)
Previously, all the queries where exposed straight on the entity type, not taking into account the bundles on which the field actually appears. This changes it to keep exposing the base fields on the entity type interface, but expose the configurable entity reference fields only on the bundles they're attached to.
1 parent 89905fd commit 3c8b237

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public function __construct(
7272
* {@inheritdoc}
7373
*/
7474
public function getDerivativeDefinitions($basePluginDefinition) {
75+
$fieldMap = $this->entityFieldManager->getFieldMap();
76+
7577
foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
7678
$interfaces = class_implements($entityType->getClass());
7779
if (!array_key_exists(FieldableEntityInterface::class, $interfaces)) {
@@ -89,8 +91,18 @@ public function getDerivativeDefinitions($basePluginDefinition) {
8991

9092
$targetType = $this->entityTypeManager->getDefinition($targetTypeId);
9193
$fieldName = $fieldDefinition->getName();
94+
95+
if ($fieldDefinition instanceof BaseFieldDefinition || !$entityType->hasKey('bundle')) {
96+
$parents = [StringHelper::camelCase($entityTypeId)];
97+
}
98+
else {
99+
$parents = [];
100+
foreach ($fieldMap[$entityTypeId][$fieldName]['bundles'] as $bundle) {
101+
$parents[] = StringHelper::camelCase($entityTypeId . '_' . $bundle);
102+
}
103+
}
92104
$derivative = [
93-
'parents' => [StringHelper::camelCase($entityTypeId)],
105+
'parents' => $parents,
94106
'name' => StringHelper::propCase('query', $fieldName),
95107
'description' => $this->t('Query reference: @description', [
96108
'@description' => $fieldDefinition->getDescription(),

0 commit comments

Comments
 (0)