Completed
Push — 8.x-1.x ( 021124...3a0ae7 )
by Sebastian
03:32
created

ViewSortByDeriver::getDerivativeDefinitions()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.439
c 0
b 0
f 0
cc 5
eloc 21
nc 2
nop 1
1
<?php
2
3
namespace Drupal\graphql_views\Plugin\Deriver\Enums;
4
5
use Drupal\graphql\Utility\StringHelper;
6
use Drupal\graphql_views\Plugin\Deriver\ViewDeriverBase;
7
use Drupal\views\Views;
8
9
class ViewSortByDeriver extends ViewDeriverBase {
10
11
  /**
12
   * {@inheritdoc}
13
   */
14
  public function getDerivativeDefinitions($basePluginDefinition) {
15
    if ($this->entityTypeManager->hasDefinition('view')) {
16
      $viewStorage = $this->entityTypeManager->getStorage('view');
17
18
      foreach (Views::getApplicableViews('graphql_display') as list($viewId, $displayId)) {
19
        /** @var \Drupal\views\ViewEntityInterface $view */
20
        $view = $viewStorage->load($viewId);
21
        if (!$type = $this->getRowResolveType($view, $displayId)) {
22
          continue;
23
        }
24
25
        /** @var \Drupal\graphql_views\Plugin\views\display\GraphQL $display */
26
        $display = $this->getViewDisplay($view, $displayId);
27
        $sorts = array_map(function ($sort) {
28
          return [
29
            'name' => $sort['id'],
30
            'value' => $sort['id'],
31
            'description' => $sort['expose']['label'],
32
          ];
33
        }, array_filter($display->getOption('sorts') ?: [], function($sort) {
34
          return $sort['exposed'];
35
        }));
36
37
        $id = implode('-', [$viewId, $displayId, 'view']);
38
        $this->derivatives["$viewId-$displayId"] = [
39
          'name' => StringHelper::camelCase($id, 'sort', 'by'),
40
          'values' => $sorts,
41
        ] + $basePluginDefinition;
42
      }
43
    }
44
45
    return parent::getDerivativeDefinitions($basePluginDefinition);
46
  }
47
48
}
49