Skip to content

Commit f680fd8

Browse files
Merge pull request #44 from rwohleb/multi-index-field-collision
Modify field derivative IDs to include index
2 parents 3904697 + 7269d07 commit f680fd8

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

src/Plugin/GraphQL/Derivers/SearchAPIFieldDeriver.php

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Drupal\Core\Entity\EntityTypeManagerInterface;
77
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
88
use Drupal\graphql_search_api\Utility\SearchAPIHelper;
9+
use Drupal\search_api\Item\FieldInterface;
910
use Symfony\Component\DependencyInjection\ContainerInterface;
1011

1112
/**
@@ -48,21 +49,27 @@ public function getDerivativeDefinitions($base_plugin_definition) {
4849
$this->derivatives['index_id']['name'] = 'index_id';
4950
$this->derivatives['index_id']['type'] = 'String';
5051

52+
/** @var \Drupal\search_api\IndexInterface $index */
5153
foreach ($indexes as $index_id => $index) {
54+
$parent = str_replace("_", "", ucwords($index_id . "Doc", '_'));
5255

5356
foreach ($index->getFields() as $field_id => $field) {
54-
55-
// Define to which Doc type variant the field belongs to.
56-
$base_plugin_definition['parents'][0] = str_replace("_", "", ucwords($index_id . "Doc", '_'));
57-
58-
// Initialising derivative settings.
59-
$this->derivatives[$field_id] = $base_plugin_definition;
60-
$this->derivatives[$field_id]['id'] = $field_id;
61-
$this->derivatives[$field_id]['name'] = $field_id;
62-
63-
// Set field type.
64-
$this->setFieldType($field, $field_id);
65-
57+
$derivative_id = implode(':', [$parent, $field_id]);
58+
if (isset($this->derivatives[$derivative_id])) {
59+
$base_plugin_definition['parents'][] = $parent;
60+
}
61+
else {
62+
// Define to which Doc type variant the field belongs to.
63+
$base_plugin_definition['parents'] = [$parent];
64+
65+
// Initialising derivative settings.
66+
$this->derivatives[$derivative_id] = $base_plugin_definition;
67+
$this->derivatives[$derivative_id]['id'] = $field_id;
68+
$this->derivatives[$derivative_id]['name'] = $field_id;
69+
70+
// Set field type.
71+
$this->setFieldType($field, $derivative_id);
72+
}
6673
}
6774
}
6875
return $this->derivatives;
@@ -71,56 +78,56 @@ public function getDerivativeDefinitions($base_plugin_definition) {
7178
/**
7279
* This method maps the field types in Search API to GraphQL types.
7380
*
74-
* @field
81+
* @param \Drupal\search_api\Item\FieldInterface $field
7582
* The field to map.
76-
* @field_id
77-
* The id of the field to map.
83+
* @param string $derivative_id
84+
* The id of the field derivative to map.
7885
*/
79-
private function setFieldType($field, $field_id) {
86+
private function setFieldType(FieldInterface $field, $derivative_id) {
8087

8188
// Get field type.
8289
$type = $field->getType();
8390

8491
// We can only check if a field is multivalue if it has a Datasource.
85-
// @Todo This seems inefficient, check when it's being cached
92+
// @todo This seems inefficient, check when it's being cached
8693
$multivalue = SearchAPIHelper::checkMultivalue($field);
8794

8895
// Map the Search API types to GraphQL.
8996
switch ($type) {
9097

9198
case 'text':
92-
$this->derivatives[$field_id]['type'] = 'String';
99+
$this->derivatives[$derivative_id]['type'] = 'String';
93100
break;
94101

95102
case 'string':
96-
$this->derivatives[$field_id]['type'] = 'String';
103+
$this->derivatives[$derivative_id]['type'] = 'String';
97104
break;
98105

99106
case 'boolean':
100-
$this->derivatives[$field_id]['type'] = 'Boolean';
107+
$this->derivatives[$derivative_id]['type'] = 'Boolean';
101108
break;
102109

103110
case 'integer':
104-
$this->derivatives[$field_id]['type'] = 'Int';
111+
$this->derivatives[$derivative_id]['type'] = 'Int';
105112
break;
106113

107114
case 'decimal':
108-
$this->derivatives[$field_id]['type'] = 'Float';
115+
$this->derivatives[$derivative_id]['type'] = 'Float';
109116
break;
110117

111118
case 'date':
112-
$this->derivatives[$field_id]['type'] = 'Timestamp';
119+
$this->derivatives[$derivative_id]['type'] = 'Timestamp';
113120
break;
114121

115122
default:
116-
$this->derivatives[$field_id]['type'] = 'String';
123+
$this->derivatives[$derivative_id]['type'] = 'String';
117124
break;
118125

119126
}
120127

121128
// If field is multivalue we set the type as an array.
122129
if ($multivalue) {
123-
$this->derivatives[$field_id]['type'] = '[' . $this->derivatives[$field_id]['type'] . ']';
130+
$this->derivatives[$derivative_id]['type'] = '[' . $this->derivatives[$derivative_id]['type'] . ']';
124131
}
125132
}
126133

src/Plugin/GraphQL/Fields/SearchAPIField.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class SearchAPIField extends FieldPluginBase {
2222
* {@inheritdoc}
2323
*/
2424
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
25-
2625
$derivative_id = $this->getDerivativeId();
26+
list($parent_type, $field_id) = explode(':', $derivative_id);
2727

2828
// Not all documents have values for all fields so we need to check.
29-
if (isset($value['item'][$derivative_id])) {
29+
if ($parent_type == $info->parentType && isset($value['item'][$field_id])) {
3030

31-
$field = $value['item'][$derivative_id];
31+
$field = $value['item'][$field_id];
3232

3333
$field_values = $field->getValues();
3434
$field_type = $field->getType();

0 commit comments

Comments
 (0)