ViewRowFieldDeriver::getDerivativeDefinitions()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 9.0328
c 0
b 0
f 0
cc 5
nc 2
nop 1
1
<?php
2
3
namespace Drupal\graphql_views\Plugin\Deriver\Fields;
4
5
use Drupal\graphql_views\Plugin\views\row\GraphQLFieldRow;
6
use Drupal\graphql_views\Plugin\Deriver\ViewDeriverBase;
7
use Drupal\views\Views;
8
9
/**
10
 * Derive row fields from configured fieldable views.
11
 */
12
class ViewRowFieldDeriver extends ViewDeriverBase {
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
        /** @var \Drupal\graphql_views\Plugin\views\display\GraphQL $display */
25
        $display = $this->getViewDisplay($view, $displayId);
26
        $rowPlugin = $display->getPlugin('row');
27
28
        // This deriver only supports our custom field row plugin.
29
        if (!$rowPlugin instanceof GraphQLFieldRow) {
30
          continue;
31
        }
32
33
        foreach ($display->getHandlers('field') as $name => $field) {
34
          $id = implode('-', [$viewId, $displayId, 'field', $name]);
35
          $alias = $rowPlugin->getFieldKeyAlias($name);
36
          $type = $rowPlugin->getFieldType($name);
37
38
          $this->derivatives[$id] = [
39
            'id' => $id,
40
            'name' => $alias,
41
            'type' => $type,
42
            'parents' => [$display->getGraphQLRowName()],
43
            'view' => $viewId,
44
            'display' => $displayId,
45
            'field' => $alias,
46
          ] + $this->getCacheMetadataDefinition($view, $display) + $basePluginDefinition;
47
        }
48
      }
49
    }
50
51
    return parent::getDerivativeDefinitions($basePluginDefinition);
52
  }
53
54
}
55