Completed
Pull Request — 8.x-3.x (#399)
by Sebastian
04:43 queued 02:12
created

ViewRowFieldDeriver::getDerivativeDefinitions()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 2
nop 1
dl 0
loc 35
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
4
5
use Drupal\graphql\Plugin\views\row\GraphQLFieldRow;
6
use Drupal\graphql\Utility\StringHelper;
7
use Drupal\graphql_core\Plugin\Deriver\ViewDeriverBase;
8
use Drupal\views\Views;
9
10
/**
11
 * Derive row fields from configured fieldable views.
12
 */
13
class ViewRowFieldDeriver extends ViewDeriverBase {
14
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function getDerivativeDefinitions($basePluginDefinition) {
19
    if ($this->entityTypeManager->hasDefinition('view')) {
20
      $viewStorage = $this->entityTypeManager->getStorage('view');
21
22
      foreach (Views::getApplicableViews('graphql_display') as list($viewId, $displayId)) {
23
        /** @var \Drupal\views\ViewEntityInterface $view */
24
        $view = $viewStorage->load($viewId);
25
        /** @var \Drupal\graphql\Plugin\views\display\GraphQL $display */
26
        $display = $this->getViewDisplay($view, $displayId);
27
        $rowPlugin = $display->getPlugin('row');
28
29
        // This deriver only supports our custom field row plugin.
30
        if (!$rowPlugin instanceof GraphQLFieldRow) {
31
          continue;
32
        }
33
34
        foreach ($display->getHandlers('field') as $name => $field) {
35
          $id = implode('-', [$viewId, $displayId, 'field', $name]);
36
          $alias = $rowPlugin->getFieldKeyAlias($name);
37
38
          $this->derivatives[$id] = [
39
              'id' => $id,
40
              'name' => $alias,
41
              'type' => 'String',
42
              'parents' => [$display->getGraphQLRowName()],
43
              'view' => $viewId,
44
              'display' => $displayId,
45
              'field' => $alias,
46
            ] + $this->getCacheMetadataDefinition($view) + $basePluginDefinition;
47
        }
48
      }
49
    }
50
51
    return parent::getDerivativeDefinitions($basePluginDefinition);
52
  }
53
54
}
55