Completed
Push — 8.x-1.x ( d2d0f5...eaac7a )
by Sebastian
01:20
created

ViewDeriver::getContextualArguments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Drupal\graphql_views\Plugin\Deriver\Fields;
4
5
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
6
use Drupal\graphql_views\Plugin\Deriver\ViewDeriverBase;
7
use Drupal\views\Views;
8
9
/**
10
 * Derive fields from configured views.
11
 */
12
class ViewDeriver extends ViewDeriverBase implements ContainerDeriverInterface {
13
14
  /**
15
   * {@inheritdoc}
16
   */
17
  public function getDerivativeDefinitions($basePluginDefinition) {
18
    if ($this->entityTypeManager->hasDefinition('view')) {
19
      $viewStorage = $this->entityTypeManager->getStorage('view');
20
21
      foreach (Views::getApplicableViews('graphql_display') as list($viewId, $displayId)) {
22
        /** @var \Drupal\views\ViewEntityInterface $view */
23
        $view = $viewStorage->load($viewId);
24
        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...
25
          continue;
26
        }
27
28
        /** @var \Drupal\graphql_views\Plugin\views\display\GraphQL $display */
29
        $display = $this->getViewDisplay($view, $displayId);
30
31
        $id = implode('-', [$viewId, $displayId, 'view']);
32
        $info = $this->getArgumentsInfo($display->getOption('arguments') ?: []);
33
        $arguments = [];
34
        $arguments += $this->getContextualArguments($info, $id);
35
        $arguments += $this->getPagerArguments($display);
36
        $arguments += $this->getSortArguments($display, $id);
37
        $arguments += $this->getFilterArguments($display, $id);
38
        $types = $this->getTypes($info);
39
40
        $this->derivatives[$id] = [
41
          'id' => $id,
42
          'name' => $display->getGraphQLQueryName(),
43
          'type' => $display->getGraphQLResultName(),
44
          'parents' => $types,
45
          'arguments' => $arguments,
46
          'view' => $viewId,
47
          'display' => $displayId,
48
          'paged' => $this->isPaged($display),
49
          'arguments_info' => $info,
50
        ] + $this->getCacheMetadataDefinition($view, $display) + $basePluginDefinition;
51
      }
52
    }
53
54
    return parent::getDerivativeDefinitions($basePluginDefinition);
55
  }
56
57
}
58