6
6
use Drupal \Core \Entity \EntityTypeManagerInterface ;
7
7
use Drupal \Core \Plugin \Discovery \ContainerDeriverInterface ;
8
8
use Drupal \graphql_search_api \Utility \SearchAPIHelper ;
9
+ use Drupal \search_api \Item \FieldInterface ;
9
10
use Symfony \Component \DependencyInjection \ContainerInterface ;
10
11
11
12
/**
14
15
class SearchAPIFieldDeriver extends DeriverBase implements ContainerDeriverInterface {
15
16
16
17
/**
18
+ * The entity type manager service.
19
+ *
17
20
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
18
21
*/
19
22
protected $ entityTypeManager ;
@@ -48,21 +51,27 @@ public function getDerivativeDefinitions($base_plugin_definition) {
48
51
$ this ->derivatives ['index_id ' ]['name ' ] = 'index_id ' ;
49
52
$ this ->derivatives ['index_id ' ]['type ' ] = 'String ' ;
50
53
54
+ /** @var \Drupal\search_api\IndexInterface $index */
51
55
foreach ($ indexes as $ index_id => $ index ) {
56
+ $ parent = str_replace ("_ " , "" , ucwords ($ index_id . "Doc " , '_ ' ));
52
57
53
58
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
-
59
+ $ derivative_id = implode (': ' , [$ parent , $ field_id ]);
60
+ if (isset ($ this ->derivatives [$ derivative_id ])) {
61
+ $ base_plugin_definition ['parents ' ][] = $ parent ;
62
+ }
63
+ else {
64
+ // Define to which Doc type variant the field belongs to.
65
+ $ base_plugin_definition ['parents ' ] = [$ parent ];
66
+
67
+ // Initialising derivative settings.
68
+ $ this ->derivatives [$ derivative_id ] = $ base_plugin_definition ;
69
+ $ this ->derivatives [$ derivative_id ]['id ' ] = $ field_id ;
70
+ $ this ->derivatives [$ derivative_id ]['name ' ] = $ field_id ;
71
+
72
+ // Set field type.
73
+ $ this ->setFieldType ($ field , $ derivative_id );
74
+ }
66
75
}
67
76
}
68
77
return $ this ->derivatives ;
@@ -71,56 +80,56 @@ public function getDerivativeDefinitions($base_plugin_definition) {
71
80
/**
72
81
* This method maps the field types in Search API to GraphQL types.
73
82
*
74
- * @field
83
+ * @param \Drupal\search_api\Item\FieldInterface $ field
75
84
* The field to map.
76
- * @field_id
77
- * The id of the field to map.
85
+ * @param string $derivative_id
86
+ * The id of the field derivative to map.
78
87
*/
79
- private function setFieldType ($ field , $ field_id ) {
88
+ private function setFieldType (FieldInterface $ field , $ derivative_id ) {
80
89
81
90
// Get field type.
82
91
$ type = $ field ->getType ();
83
92
84
93
// 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
94
+ // @todo This seems inefficient, check when it's being cached
86
95
$ multivalue = SearchAPIHelper::checkMultivalue ($ field );
87
96
88
97
// Map the Search API types to GraphQL.
89
98
switch ($ type ) {
90
99
91
100
case 'text ' :
92
- $ this ->derivatives [$ field_id ]['type ' ] = 'String ' ;
101
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'String ' ;
93
102
break ;
94
103
95
104
case 'string ' :
96
- $ this ->derivatives [$ field_id ]['type ' ] = 'String ' ;
105
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'String ' ;
97
106
break ;
98
107
99
108
case 'boolean ' :
100
- $ this ->derivatives [$ field_id ]['type ' ] = 'Boolean ' ;
109
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'Boolean ' ;
101
110
break ;
102
111
103
112
case 'integer ' :
104
- $ this ->derivatives [$ field_id ]['type ' ] = 'Int ' ;
113
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'Int ' ;
105
114
break ;
106
115
107
116
case 'decimal ' :
108
- $ this ->derivatives [$ field_id ]['type ' ] = 'Float ' ;
117
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'Float ' ;
109
118
break ;
110
119
111
120
case 'date ' :
112
- $ this ->derivatives [$ field_id ]['type ' ] = 'Timestamp ' ;
121
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'Timestamp ' ;
113
122
break ;
114
123
115
124
default :
116
- $ this ->derivatives [$ field_id ]['type ' ] = 'String ' ;
125
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'String ' ;
117
126
break ;
118
127
119
128
}
120
129
121
130
// If field is multivalue we set the type as an array.
122
131
if ($ multivalue ) {
123
- $ this ->derivatives [$ field_id ]['type ' ] = '[ ' . $ this ->derivatives [$ field_id ]['type ' ] . '] ' ;
132
+ $ this ->derivatives [$ derivative_id ]['type ' ] = '[ ' . $ this ->derivatives [$ derivative_id ]['type ' ] . '] ' ;
124
133
}
125
134
}
126
135
0 commit comments