|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file |
|
5
|
|
|
* Contains \Drupal\graphql\Plugin\views\display |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Drupal\graphql_views\Plugin\views\display; |
|
9
|
|
|
|
|
10
|
|
|
use Drupal\Core\Cache\CacheableMetadata; |
|
11
|
|
|
use Drupal\graphql\Utility\StringHelper; |
|
12
|
|
|
use Drupal\views\Plugin\views\display\DisplayPluginBase; |
|
13
|
|
|
use Drupal\Core\Form\FormStateInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Provides a display plugin for GraphQL views. |
|
17
|
|
|
* |
|
18
|
|
|
* @ViewsDisplay( |
|
19
|
|
|
* id = "graphql", |
|
20
|
|
|
* title = @Translation("GraphQL"), |
|
21
|
|
|
* help = @Translation("Creates a GraphQL entity list display."), |
|
22
|
|
|
* admin = @Translation("GraphQL"), |
|
23
|
|
|
* graphql_display = TRUE, |
|
24
|
|
|
* returns_response = TRUE |
|
25
|
|
|
* ) |
|
26
|
|
|
*/ |
|
27
|
|
|
class GraphQL extends DisplayPluginBase { |
|
28
|
|
|
/** |
|
29
|
|
|
* Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAJAX. |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $usesAJAX = FALSE; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesPager. |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $usesPager = FALSE; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesMore. |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $usesMore = FALSE; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAreas. |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $usesAreas = FALSE; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesOptions. |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $usesOptions = TRUE; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getType() { |
|
57
|
|
|
return 'graphql'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function usesFields() { |
|
64
|
|
|
return TRUE; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function usesExposed() { |
|
71
|
|
|
return TRUE; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function displaysExposed() { |
|
78
|
|
|
return FALSE; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function defineOptions() { |
|
85
|
|
|
$options = parent::defineOptions(); |
|
86
|
|
|
|
|
87
|
|
|
// Set the default plugins to 'graphql'. |
|
88
|
|
|
$options['style']['contains']['type']['default'] = 'graphql'; |
|
89
|
|
|
$options['exposed_form']['contains']['type']['default'] = 'graphql'; |
|
90
|
|
|
$options['row']['contains']['type']['default'] = 'graphql_entity'; |
|
91
|
|
|
|
|
92
|
|
|
$options['defaults']['default']['style'] = FALSE; |
|
93
|
|
|
$options['defaults']['default']['exposed_form'] = FALSE; |
|
94
|
|
|
$options['defaults']['default']['row'] = FALSE; |
|
95
|
|
|
|
|
96
|
|
|
// Remove css/exposed form settings, as they are not used for the data display. |
|
97
|
|
|
unset($options['exposed_block']); |
|
98
|
|
|
unset($options['css_class']); |
|
99
|
|
|
|
|
100
|
|
|
$options['graphql_query_name'] = ['default' => '']; |
|
101
|
|
|
return $options; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the user defined query name or the default one. |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
* Query name. |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getGraphQLQueryName() { |
|
111
|
|
|
return $this->getGraphQLName(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Gets the result name. |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
* Result name. |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getGraphQLResultName() { |
|
121
|
|
|
return $this->getGraphQLName('result', TRUE); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Gets the row name. |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
* Row name. |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getGraphQLRowName() { |
|
131
|
|
|
return $this->getGraphQLName('row', TRUE); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Gets the filter input name. |
|
136
|
|
|
* |
|
137
|
|
|
* @return string |
|
138
|
|
|
* Filter name. |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getGraphQLFilterInputName() { |
|
141
|
|
|
return $this->getGraphQLName('filter_input', TRUE); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Gets the contextual filter input name. |
|
146
|
|
|
* |
|
147
|
|
|
* @return string |
|
148
|
|
|
* Contextual filter name. |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getGraphQLContextualFilterInputName() { |
|
151
|
|
|
return $this->getGraphQLName('contextual_filter_input', TRUE); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Gets the sort by name. |
|
156
|
|
|
* |
|
157
|
|
|
* @return string |
|
158
|
|
|
* Sort by name. |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getGraphQLSortByName() { |
|
161
|
|
|
return $this->getGraphQLName('sort_by', TRUE); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns the formatted name. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string|null $suffix |
|
168
|
|
|
* Id suffix, eg. row, result. |
|
169
|
|
|
* @param bool $type |
|
170
|
|
|
* Whether to use camel- or snake case. Uses camel case if TRUE. Defaults to |
|
171
|
|
|
* FALSE. |
|
172
|
|
|
* |
|
173
|
|
|
* @return string The id. |
|
174
|
|
|
* The id. |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getGraphQLName($suffix = NULL, $type = FALSE) { |
|
177
|
|
|
$queryName = strip_tags($this->getOption('graphql_query_name')); |
|
178
|
|
|
|
|
179
|
|
|
if (empty($queryName)) { |
|
180
|
|
|
$viewId = $this->view->id(); |
|
181
|
|
|
$displayId = $this->display['id']; |
|
182
|
|
|
$parts = [$viewId, $displayId, 'view', $suffix]; |
|
183
|
|
|
return $type ? call_user_func_array([StringHelper::class, 'camelCase'], $parts) : call_user_func_array([StringHelper::class, 'propCase'], $parts); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$parts = array_filter([$queryName, $suffix]); |
|
187
|
|
|
return $type ? call_user_func_array([StringHelper::class, 'camelCase'], $parts) : call_user_func_array([StringHelper::class, 'propCase'], $parts); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* {@inheritdoc} |
|
192
|
|
|
*/ |
|
193
|
|
|
public function optionsSummary(&$categories, &$options) { |
|
194
|
|
|
parent::optionsSummary($categories, $options); |
|
195
|
|
|
|
|
196
|
|
|
unset($categories['title']); |
|
197
|
|
|
unset($categories['pager'], $categories['exposed'], $categories['access']); |
|
198
|
|
|
|
|
199
|
|
|
unset($options['show_admin_links'], $options['analyze-theme'], $options['link_display']); |
|
200
|
|
|
unset($options['show_admin_links'], $options['analyze-theme'], $options['link_display']); |
|
201
|
|
|
|
|
202
|
|
|
unset($options['title'], $options['access']); |
|
203
|
|
|
unset($options['exposed_block'], $options['css_class']); |
|
204
|
|
|
unset($options['query'], $options['group_by']); |
|
205
|
|
|
|
|
206
|
|
|
$categories['graphql'] = [ |
|
207
|
|
|
'title' => $this->t('GraphQL'), |
|
208
|
|
|
'column' => 'second', |
|
209
|
|
|
'build' => [ |
|
210
|
|
|
'#weight' => -10, |
|
211
|
|
|
], |
|
212
|
|
|
]; |
|
213
|
|
|
|
|
214
|
|
|
$options['graphql_query_name'] = [ |
|
215
|
|
|
'category' => 'graphql', |
|
216
|
|
|
'title' => $this->t('Query name'), |
|
217
|
|
|
'value' => views_ui_truncate($this->getGraphQLQueryName(), 24), |
|
218
|
|
|
]; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* {@inheritdoc} |
|
223
|
|
|
*/ |
|
224
|
|
|
public function buildOptionsForm(&$form, FormStateInterface $form_state) { |
|
225
|
|
|
parent::buildOptionsForm($form, $form_state); |
|
226
|
|
|
|
|
227
|
|
|
switch ($form_state->get('section')) { |
|
228
|
|
|
case 'graphql_query_name': |
|
229
|
|
|
$form['#title'] .= $this->t('Query name'); |
|
230
|
|
|
$form['graphql_query_name'] = [ |
|
231
|
|
|
'#type' => 'textfield', |
|
232
|
|
|
'#description' => $this->t('This will be the graphQL query name.'), |
|
233
|
|
|
'#default_value' => $this->getGraphQLQueryName(), |
|
234
|
|
|
]; |
|
235
|
|
|
break; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* {@inheritdoc} |
|
241
|
|
|
*/ |
|
242
|
|
|
public function submitOptionsForm(&$form, FormStateInterface $form_state) { |
|
243
|
|
|
parent::submitOptionsForm($form, $form_state); |
|
244
|
|
|
$section = $form_state->get('section'); |
|
245
|
|
|
switch ($section) { |
|
246
|
|
|
case 'graphql_query_name': |
|
247
|
|
|
$this->setOption($section, $form_state->getValue($section)); |
|
248
|
|
|
break; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* {@inheritdoc} |
|
254
|
|
|
*/ |
|
255
|
|
|
public function execute() { |
|
256
|
|
|
return $this->view->execute(); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* {@inheritdoc} |
|
261
|
|
|
*/ |
|
262
|
|
|
public function render() { |
|
263
|
|
|
$rows = (!empty($this->view->result) || $this->view->style_plugin->evenEmpty()) ? $this->view->style_plugin->render($this->view->result) : []; |
|
264
|
|
|
|
|
265
|
|
|
// Apply the cache metadata from the display plugin. This comes back as a |
|
266
|
|
|
// cache render array so we have to transform it back afterwards. |
|
267
|
|
|
$this->applyDisplayCachablityMetadata($this->view->element); |
|
268
|
|
|
|
|
269
|
|
|
return [ |
|
270
|
|
|
'view' => $this->view, |
|
271
|
|
|
'rows' => $rows, |
|
272
|
|
|
'cache' => CacheableMetadata::createFromRenderArray($this->view->element), |
|
273
|
|
|
]; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|