Completed
Pull Request — 8.x-1.x (#22)
by Sebastian
02:52 queued 01:21
created

ViewDerivative   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveValues() 0 12 2
A setViewDefaultValues() 0 8 5
1
<?php
2
3
namespace Drupal\graphql_views\Plugin\GraphQL\Fields\Entity\Fields\View;
4
5
use Drupal\graphql\GraphQL\Execution\ResolveContext;
6
use Drupal\graphql_views\Plugin\GraphQL\Fields\View;
7
use Drupal\graphql_views\ViewDeriverHelperTrait;
8
use Drupal\views\Entity\View as EntityView;
9
use Drupal\views\Plugin\views\display\DisplayPluginInterface;
10
use GraphQL\Type\Definition\ResolveInfo;
11
12
/**
13
 * Retrieve the views field derivative.
14
 *
15
 * @GraphQLField(
16
 *   id = "view_derivative",
17
 *   secure = true,
18
 *   name = "viewDerivative",
19
 *   type = "ViewResult",
20
 *   field_types = {"viewsreference"},
21
 *   provider = "views",
22
 *   arguments={
23
 *     "filter" = {
24
 *       "optional" = true,
25
 *       "type" = "Untyped"
26
 *     },
27
 *     "page" = {
28
 *       "optional" = true,
29
 *       "type" = "Int"
30
 *     },
31
 *     "pageSize" = {
32
 *       "optional" = true,
33
 *       "type" = "Int"
34
 *     },
35
 *     "sortBy" = {
36
 *       "optional" = true,
37
 *       "type" = "Untyped"
38
 *     },
39
 *     "sortDirection" = {
40
 *       "optional" = true,
41
 *       "type" = "ViewSortDirection",
42
 *       "default" = "asc"
43
 *     },
44
 *     "contextualFilter" = {
45
 *       "optional" = true,
46
 *       "type" = "Untyped"
47
 *     }
48
 *   },
49
 *   deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver"
50
 * )
51
 */
52
class ViewDerivative extends View {
53
  use ViewDeriverHelperTrait;
54
55
  /**
56
   * {@inheritdoc}
57
   */
58
  public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
59
    $values = $value->getValue();
60
    $this->pluginDefinition['view'] = $values['target_id'];
61
    $this->pluginDefinition['display'] = $values['display_id'];
62
    $view = EntityView::load($values['target_id']);
63
    $display = $this->getViewDisplay($view, $values['display_id']);
64
    $this->pluginDefinition['paged'] = $this->isPaged($display);
65
    $this->pluginDefinition['argument_info'] = $this->getArgumentsInfo($display->getOption('arguments') ?: []);
66
    $this->pluginDefinition = array_merge($this->pluginDefinition, $this->getCacheMetadataDefinition($view, $display));
67
    $this->setViewDefaultValues($display, $args);
68
    return parent::resolveValues($value, $args, $context, $info);
69
  }
70
71
  /**
72
   * Set default display settings.
73
   *
74
   * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display
75
   *   The display configuration.
76
   * @param array $args
77
   *   Arguments where the default view settings needs to be added.
78
   */
79
  protected function setViewDefaultValues(DisplayPluginInterface $display, array &$args) {
80
    if (!isset($args['pageSize']) && $this->pluginDefinition['paged']) {
81
      $args['pageSize'] = $this->getPagerLimit($display);
82
    }
83
    if (!isset($args['page']) && $this->pluginDefinition['paged']) {
84
      $args['page'] = $this->getPagerOffset($display);
85
    }
86
  }
87
88
}
89