Completed
Push — 8.x-3.x ( f0f390...6b97f3 )
by Sebastian
02:54
created

GraphQL::getGraphQLName()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 13
rs 9.2
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\graphql\Plugin\views\display
6
 */
7
8
namespace Drupal\graphql\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
   *   Result 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
   *   Result name.
149
   */
150
  public function getGraphQLContextualFilterInputName() {
151
    return $this->getGraphQLName('contextual_filter_input', TRUE);
152
  }
153
154
  /**
155
   * Returns the formatted name.
156
   *
157
   * @param string|null $suffix
158
   *   Id suffix, eg. row, result.
159
   * @param bool $type
160
   *   Whether to use camel- or snake case. Uses camel case if TRUE. Defaults to
161
   *   FALSE.
162
   *
163
   * @return string The id.
164
   *   The id.
165
   */
166
  public function getGraphQLName($suffix = NULL, $type = FALSE) {
167
    $queryName = strip_tags($this->getOption('graphql_query_name'));
168
169
    if (empty($queryName)) {
170
      $viewId = $this->view->id();
171
      $displayId = $this->display['id'];
172
      $parts = [$viewId, $displayId, 'view', $suffix];
173
      return $type ? StringHelper::camelCase($parts) : StringHelper::propCase($parts);
174
    }
175
176
    $parts = array_filter([$queryName, $suffix]);
177
    return $type ? StringHelper::camelCase($parts) : StringHelper::propCase($parts);
178
  }
179
180
  /**
181
   * {@inheritdoc}
182
   */
183
  public function optionsSummary(&$categories, &$options) {
184
    parent::optionsSummary($categories, $options);
185
186
    unset($categories['title']);
187
    unset($categories['pager'], $categories['exposed'], $categories['access']);
188
189
    unset($options['show_admin_links'], $options['analyze-theme'], $options['link_display']);
190
    unset($options['show_admin_links'], $options['analyze-theme'], $options['link_display']);
191
192
    unset($options['title'], $options['access']);
193
    unset($options['exposed_block'], $options['css_class']);
194
    unset($options['query'], $options['group_by']);
195
196
    $categories['graphql'] = [
197
      'title' => $this->t('GraphQL'),
198
      'column' => 'second',
199
      'build' => [
200
        '#weight' => -10,
201
      ],
202
    ];
203
204
    $options['graphql_query_name'] = [
205
      'category' => 'graphql',
206
      'title' => $this->t('Query name'),
207
      'value' => views_ui_truncate($this->getGraphQLQueryName(), 24),
208
    ];
209
  }
210
211
  /**
212
   * {@inheritdoc}
213
   */
214
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
215
    parent::buildOptionsForm($form, $form_state);
216
217
    switch ($form_state->get('section')) {
218
      case 'graphql_query_name':
219
        $form['#title'] .= $this->t('Query name');
220
        $form['graphql_query_name'] = [
221
          '#type' => 'textfield',
222
          '#description' => $this->t('This will be the graphQL query name.'),
223
          '#default_value' => $this->getGraphQLQueryName(),
224
        ];
225
        break;
226
    }
227
  }
228
229
  /**
230
   * {@inheritdoc}
231
   */
232
  public function submitOptionsForm(&$form, FormStateInterface $form_state) {
233
    parent::submitOptionsForm($form, $form_state);
234
    $section = $form_state->get('section');
235
    switch ($section) {
236
      case 'graphql_query_name':
237
        $this->setOption($section, $form_state->getValue($section));
238
        break;
239
    }
240
  }
241
242
  /**
243
   * {@inheritdoc}
244
   */
245
  public function execute() {
246
    return $this->view->execute();
247
  }
248
249
  /**
250
   * {@inheritdoc}
251
   */
252
  public function render() {
253
    $rows = (!empty($this->view->result) || $this->view->style_plugin->evenEmpty()) ? $this->view->style_plugin->render($this->view->result) : [];
254
255
    // Apply the cache metadata from the display plugin. This comes back as a
256
    // cache render array so we have to transform it back afterwards.
257
    $this->applyDisplayCachablityMetadata($this->view->element);
258
259
    return [
260
      'view' => $this->view,
261
      'rows' => $rows,
262
      'cache' => CacheableMetadata::createFromRenderArray($this->view->element),
263
    ];
264
  }
265
}
266