|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql_views\Plugin\Deriver\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; |
|
6
|
|
|
use Drupal\graphql\Utility\StringHelper; |
|
7
|
|
|
use Drupal\graphql_views\Plugin\Deriver\ViewDeriverBase; |
|
8
|
|
|
use Drupal\views\Plugin\views\display\DisplayPluginInterface; |
|
9
|
|
|
use Drupal\views\Views; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Derive fields from configured views. |
|
13
|
|
|
*/ |
|
14
|
|
|
class ViewDeriver extends ViewDeriverBase implements ContainerDeriverInterface { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritdoc} |
|
18
|
|
|
*/ |
|
19
|
|
|
public function getDerivativeDefinitions($basePluginDefinition) { |
|
20
|
|
|
if ($this->entityTypeManager->hasDefinition('view')) { |
|
21
|
|
|
$viewStorage = $this->entityTypeManager->getStorage('view'); |
|
22
|
|
|
|
|
23
|
|
|
foreach (Views::getApplicableViews('graphql_display') as list($viewId, $displayId)) { |
|
24
|
|
|
/** @var \Drupal\views\ViewEntityInterface $view */ |
|
25
|
|
|
$view = $viewStorage->load($viewId); |
|
26
|
|
|
if (!$this->getRowResolveType($view, $displayId)) { |
|
|
|
|
|
|
27
|
|
|
continue; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** @var \Drupal\graphql_views\Plugin\views\display\GraphQL $display */ |
|
31
|
|
|
$display = $this->getViewDisplay($view, $displayId); |
|
32
|
|
|
|
|
33
|
|
|
$id = implode('-', [$viewId, $displayId, 'view']); |
|
34
|
|
|
$info = $this->getArgumentsInfo($display->getOption('arguments') ?: []); |
|
35
|
|
|
$arguments = []; |
|
36
|
|
|
$arguments += $this->getContextualArguments($info, $id); |
|
37
|
|
|
$arguments += $this->getPagerArguments($display); |
|
38
|
|
|
$arguments += $this->getSortArguments($display, $id); |
|
39
|
|
|
$arguments += $this->getFilterArguments($display, $id); |
|
40
|
|
|
$types = $this->getTypes($display, $info); |
|
41
|
|
|
|
|
42
|
|
|
$this->derivatives[$id] = [ |
|
43
|
|
|
'id' => $id, |
|
44
|
|
|
'name' => $display->getGraphQLQueryName(), |
|
45
|
|
|
'type' => $display->getGraphQLResultName(), |
|
46
|
|
|
'parents' => $types, |
|
47
|
|
|
'arguments' => $arguments, |
|
48
|
|
|
'view' => $viewId, |
|
49
|
|
|
'display' => $displayId, |
|
50
|
|
|
'paged' => $this->isPaged($display), |
|
51
|
|
|
'arguments_info' => $info, |
|
52
|
|
|
] + $this->getCacheMetadataDefinition($view, $display) + $basePluginDefinition; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return parent::getDerivativeDefinitions($basePluginDefinition); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Helper function to return the contextual filter argument if any exist. |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $arguments |
|
63
|
|
|
* The array of available arguments. |
|
64
|
|
|
* @param $id |
|
65
|
|
|
* The plugin derivative id. |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
* The contextual filter argument if applicable. |
|
69
|
|
|
*/ |
|
70
|
|
View Code Duplication |
protected function getContextualArguments(array $arguments, $id) { |
|
|
|
|
|
|
71
|
|
|
if (!empty($arguments)) { |
|
72
|
|
|
return [ |
|
73
|
|
|
'contextualFilter' => [ |
|
74
|
|
|
'type' => StringHelper::camelCase($id, 'contextual', 'filter', 'input'), |
|
75
|
|
|
], |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return []; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Helper function to retrieve the sort arguments if any are exposed. |
|
84
|
|
|
* |
|
85
|
|
|
* @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
|
86
|
|
|
* The display plugin. |
|
87
|
|
|
* @param $id |
|
88
|
|
|
* The plugin derivative id. |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
* The sort arguments if any exposed sorts are available. |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
protected function getSortArguments(DisplayPluginInterface $display, $id) { |
|
|
|
|
|
|
94
|
|
|
$sorts = array_filter($display->getOption('sorts') ?: [], function ($sort) { |
|
95
|
|
|
return $sort['exposed']; |
|
96
|
|
|
}); |
|
97
|
|
|
return $sorts ? [ |
|
98
|
|
|
'sortDirection' => [ |
|
99
|
|
|
'type' => 'ViewSortDirection', |
|
100
|
|
|
'default' => 'asc', |
|
101
|
|
|
], |
|
102
|
|
|
'sortBy' => [ |
|
103
|
|
|
'type' => StringHelper::camelCase($id, 'sort', 'by'), |
|
104
|
|
|
], |
|
105
|
|
|
] : []; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Helper function to return the filter argument if applicable. |
|
110
|
|
|
* |
|
111
|
|
|
* @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
|
112
|
|
|
* The display plugin. |
|
113
|
|
|
* @param $id |
|
114
|
|
|
* The plugin derivative id. |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
* The filter argument if any exposed filters are available. |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
protected function getFilterArguments(DisplayPluginInterface $display, $id) { |
|
|
|
|
|
|
120
|
|
|
$filters = array_filter($display->getOption('filters') ?: [], function($filter) { |
|
121
|
|
|
return array_key_exists('exposed', $filter) && $filter['exposed']; |
|
122
|
|
|
}); |
|
123
|
|
|
|
|
124
|
|
|
return !empty($filters) ? [ |
|
125
|
|
|
'filter' => [ |
|
126
|
|
|
'type' => $display->getGraphQLFilterInputName(), |
|
127
|
|
|
], |
|
128
|
|
|
] : []; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Helper function to retrieve the pager arguments if the display is paged. |
|
133
|
|
|
* |
|
134
|
|
|
* @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
|
135
|
|
|
* The display plugin. |
|
136
|
|
|
* |
|
137
|
|
|
* @return array |
|
138
|
|
|
* An array of pager arguments if the view display is paged. |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function getPagerArguments(DisplayPluginInterface $display) { |
|
141
|
|
|
return $this->isPaged($display) ? [ |
|
142
|
|
|
'page' => ['type' => 'Int', 'default' => $this->getPagerOffset($display)], |
|
143
|
|
|
'pageSize' => ['type' => 'Int', 'default' => $this->getPagerLimit($display)], |
|
144
|
|
|
] : []; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Helper function to retrieve the types that the view can be attached to. |
|
149
|
|
|
* |
|
150
|
|
|
* @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
|
151
|
|
|
* The display plugin. |
|
152
|
|
|
* @param array $arguments |
|
153
|
|
|
* An array containing information about the available arguments. |
|
154
|
|
|
* @return array |
|
155
|
|
|
* An array of additional types the view can be embedded in. |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function getTypes(DisplayPluginInterface $display, array $arguments) { |
|
158
|
|
|
$types = ['Root']; |
|
159
|
|
|
|
|
160
|
|
|
if (($entity_type = $display->getOption('entity_type'))) { |
|
161
|
|
|
$types = array_merge($types, [StringHelper::camelCase($entity_type)]); |
|
162
|
|
|
|
|
163
|
|
|
if (($bundles = $display->getOption('bundles'))) { |
|
164
|
|
|
$types = array_merge($types, array_map(function ($bundle) use ($entity_type) { |
|
165
|
|
|
return StringHelper::camelCase($entity_type, $bundle); |
|
166
|
|
|
}, $bundles)); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if (empty($arguments)) { |
|
171
|
|
|
return $types; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
View Code Duplication |
foreach ($arguments as $argument) { |
|
|
|
|
|
|
175
|
|
|
// Depending on whether bundles are known, we expose the view field |
|
176
|
|
|
// either on the interface (e.g. Node) or on the type (e.g. NodePage) |
|
177
|
|
|
// level. Here we specify types managed by other graphql_* modules, |
|
178
|
|
|
// yet we don't define these modules as dependencies. If types are not |
|
179
|
|
|
// in the schema, the resulting GraphQL field will be attached to |
|
180
|
|
|
// nowhere, so it won't go into the schema. |
|
181
|
|
|
if (empty($argument['bundles']) && empty($argument['entity_type'])) { |
|
182
|
|
|
continue; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if (empty($argument['bundles'])) { |
|
186
|
|
|
$types = array_merge($types, [StringHelper::camelCase($argument['entity_type'])]); |
|
187
|
|
|
} |
|
188
|
|
|
else { |
|
189
|
|
|
$types = array_merge($types, array_map(function ($bundle) use ($argument) { |
|
190
|
|
|
return StringHelper::camelCase($argument['entity_type'], $bundle); |
|
191
|
|
|
}, array_keys($argument['bundles']))); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return $types; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: