1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_views\Plugin\GraphQL\DataProducer; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
|
|
|
6
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
|
|
|
7
|
|
|
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase; |
|
|
|
|
8
|
|
|
use Drupal\views\Plugin\views\display\DisplayPluginInterface; |
|
|
|
|
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* GraphQL data producer for views. |
13
|
|
|
* |
14
|
|
|
* @DataProducer( |
15
|
|
|
* id = "views", |
16
|
|
|
* name = @Translation("Views"), |
17
|
|
|
* description = @Translation("Views."), |
18
|
|
|
* produces = @ContextDefinition("entity", |
19
|
|
|
* label = @Translation("Entity") |
20
|
|
|
* ), |
21
|
|
|
* consumes = { |
22
|
|
|
* "view_id" = @ContextDefinition("string", |
23
|
|
|
* label = @Translation("View ID") |
24
|
|
|
* ), |
25
|
|
|
* "display_id" = @ContextDefinition("string", |
26
|
|
|
* label = @Translation("Display ID") |
27
|
|
|
* ), |
28
|
|
|
* "offset" = @ContextDefinition("integer", |
29
|
|
|
* label = @Translation("Offset"), |
30
|
|
|
* required = FALSE |
31
|
|
|
* ), |
32
|
|
|
* "page_size" = @ContextDefinition("integer", |
33
|
|
|
* label = @Translation("Page size"), |
34
|
|
|
* required = FALSE |
35
|
|
|
* ), |
36
|
|
|
* "page" = @ContextDefinition("integer", |
37
|
|
|
* label = @Translation("Current page"), |
38
|
|
|
* required = FALSE |
39
|
|
|
* ) |
40
|
|
|
* } |
41
|
|
|
* ) |
42
|
|
|
*/ |
43
|
|
|
class Views extends DataProducerPluginBase implements ContainerFactoryPluginInterface { |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The entity type manager. |
47
|
|
|
* |
48
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
49
|
|
|
*/ |
50
|
|
|
protected $entityTypeManager; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
56
|
|
|
return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity_type.manager')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager) { |
63
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
64
|
|
|
$this->entityTypeManager = $entityTypeManager; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Resolve the data. |
69
|
|
|
* |
70
|
|
|
* @param string $view_id |
71
|
|
|
* The view ID. |
72
|
|
|
* @param string $display_id |
73
|
|
|
* The display ID. |
74
|
|
|
* @param int|null $offset |
75
|
|
|
* Offset of the query. |
76
|
|
|
* @param int|null $page_size |
77
|
|
|
* Number of items on page. |
78
|
|
|
* @param int|null $page |
79
|
|
|
* Number of page. |
80
|
|
|
* |
81
|
|
|
* @return array|null |
82
|
|
|
* List of entities. |
83
|
|
|
* |
84
|
|
|
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException |
85
|
|
|
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException |
86
|
|
|
*/ |
87
|
|
|
public function resolve(string $view_id, string $display_id, int $offset = NULL, int $page_size = NULL, int $page = NULL) { |
88
|
|
|
|
89
|
|
|
/** @var \Drupal\views\Entity\View $view */ |
90
|
|
|
$view = \Drupal::entityTypeManager()->getStorage('view')->load($view_id); |
91
|
|
|
|
92
|
|
|
$executable = $view->getExecutable(); |
93
|
|
|
$executable->setDisplay($display_id); |
94
|
|
|
|
95
|
|
|
// Set paging parameters. |
96
|
|
|
if ($this->isPaged($executable->getDisplay()) && $page_size && $page) { |
|
|
|
|
97
|
|
|
$executable->setItemsPerPage($page_size); |
98
|
|
|
$executable->setCurrentPage($page); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($offset) { |
|
|
|
|
102
|
|
|
$executable->setOffset($offset); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$executable->preExecute(); |
106
|
|
|
$executable->execute(); |
107
|
|
|
return $executable->render($display_id); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Check if a pager is configured. |
112
|
|
|
* |
113
|
|
|
* @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
114
|
|
|
* The display configuration. |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
* Flag indicating if the view is configured with a pager. |
118
|
|
|
*/ |
119
|
|
|
protected function isPaged(DisplayPluginInterface $display) { |
120
|
|
|
$pagerOptions = $display->getOption('pager'); |
121
|
|
|
return isset($pagerOptions['type']) && in_array($pagerOptions['type'], [ |
122
|
|
|
'full', |
123
|
|
|
'mini', |
124
|
|
|
]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths