Completed
Pull Request — 8.x-1.x (#20)
by
unknown
01:11
created

ViewDeriver::getPagerArguments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getRowResolveType($view, $displayId) of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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