ViewSortByDeriver::getDerivativeDefinitions()   B
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.7217
c 0
b 0
f 0
cc 6
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_filter($display->getOption('sorts') ?: [], function ($sort) {
28
          return $sort['exposed'];
29
        });
30
        $sorts = array_reduce($sorts, function ($carry, $sort) {
31
          $carry[strtoupper($sort['id'])] = [
32
            'value' => $sort['id'],
33
            'description' => $sort['expose']['label'],
34
          ];
35
          return $carry;
36
        }, []);
37
38
        if (!empty($sorts)) {
39
          $id = implode('-', [$viewId, $displayId, 'view']);
40
          $this->derivatives["$viewId-$displayId"] = [
41
            'name' => StringHelper::camelCase($id, 'sort', 'by'),
42
            'values' => $sorts,
43
          ] + $basePluginDefinition;
44
        }
45
      }
46
    }
47
48
    return parent::getDerivativeDefinitions($basePluginDefinition);
49
  }
50
51
}
52