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
/**
@@ -48,21 +49,27 @@ public function getDerivativeDefinitions($base_plugin_definition) {
48
49
$ this ->derivatives ['index_id ' ]['name ' ] = 'index_id ' ;
49
50
$ this ->derivatives ['index_id ' ]['type ' ] = 'String ' ;
50
51
52
+ /** @var \Drupal\search_api\IndexInterface $index */
51
53
foreach ($ indexes as $ index_id => $ index ) {
54
+ $ parent = str_replace ("_ " , "" , ucwords ($ index_id . "Doc " , '_ ' ));
52
55
53
56
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
+ }
66
73
}
67
74
}
68
75
return $ this ->derivatives ;
@@ -71,56 +78,56 @@ public function getDerivativeDefinitions($base_plugin_definition) {
71
78
/**
72
79
* This method maps the field types in Search API to GraphQL types.
73
80
*
74
- * @field
81
+ * @param \Drupal\search_api\Item\FieldInterface $ field
75
82
* 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.
78
85
*/
79
- private function setFieldType ($ field , $ field_id ) {
86
+ private function setFieldType (FieldInterface $ field , $ derivative_id ) {
80
87
81
88
// Get field type.
82
89
$ type = $ field ->getType ();
83
90
84
91
// 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
86
93
$ multivalue = SearchAPIHelper::checkMultivalue ($ field );
87
94
88
95
// Map the Search API types to GraphQL.
89
96
switch ($ type ) {
90
97
91
98
case 'text ' :
92
- $ this ->derivatives [$ field_id ]['type ' ] = 'String ' ;
99
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'String ' ;
93
100
break ;
94
101
95
102
case 'string ' :
96
- $ this ->derivatives [$ field_id ]['type ' ] = 'String ' ;
103
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'String ' ;
97
104
break ;
98
105
99
106
case 'boolean ' :
100
- $ this ->derivatives [$ field_id ]['type ' ] = 'Boolean ' ;
107
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'Boolean ' ;
101
108
break ;
102
109
103
110
case 'integer ' :
104
- $ this ->derivatives [$ field_id ]['type ' ] = 'Int ' ;
111
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'Int ' ;
105
112
break ;
106
113
107
114
case 'decimal ' :
108
- $ this ->derivatives [$ field_id ]['type ' ] = 'Float ' ;
115
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'Float ' ;
109
116
break ;
110
117
111
118
case 'date ' :
112
- $ this ->derivatives [$ field_id ]['type ' ] = 'Timestamp ' ;
119
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'Timestamp ' ;
113
120
break ;
114
121
115
122
default :
116
- $ this ->derivatives [$ field_id ]['type ' ] = 'String ' ;
123
+ $ this ->derivatives [$ derivative_id ]['type ' ] = 'String ' ;
117
124
break ;
118
125
119
126
}
120
127
121
128
// If field is multivalue we set the type as an array.
122
129
if ($ multivalue ) {
123
- $ this ->derivatives [$ field_id ]['type ' ] = '[ ' . $ this ->derivatives [$ field_id ]['type ' ] . '] ' ;
130
+ $ this ->derivatives [$ derivative_id ]['type ' ] = '[ ' . $ this ->derivatives [$ derivative_id ]['type ' ] . '] ' ;
124
131
}
125
132
}
126
133
0 commit comments