-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathSubView.php
201 lines (176 loc) · 6.18 KB
/
SubView.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
namespace Drupal\graphql_views\Plugin\GraphQL\Fields;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use GraphQL\Type\Definition\ResolveInfo;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Expose views as root fields.
*
* @GraphQLField(
* id = "subview",
* secure = true,
* parents = {"Root"},
* provider = "views",
* deriver = "Drupal\graphql_views\Plugin\Deriver\Fields\SubViewDeriver"
* )
*/
class SubView extends FieldPluginBase implements ContainerFactoryPluginInterface {
use DependencySerializationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct(
array $configuration,
$pluginId,
$pluginDefinition,
EntityTypeManagerInterface $entityTypeManager
) {
$this->entityTypeManager = $entityTypeManager;
parent::__construct($configuration, $pluginId, $pluginDefinition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static(
$configuration,
$pluginId,
$pluginDefinition,
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
$storage = $this->entityTypeManager->getStorage('view');
$definition = $this->getPluginDefinition();
/** @var \Drupal\views\Entity\View $view */
if ($view = $storage->load($definition['view'])) {
$executable = $view->getExecutable();
$executable->setDisplay($definition['display']);
/** @var \Drupal\graphql_views\Plugin\views\display\GraphQL $display */
$display = $executable->getDisplay($definition['display']);
// a subview can only work on an entity, so return null it it is not.
if (!$value instanceof EntityInterface) {
return;
}
// Set the first argument to the id of the current entity.
$executable->setArguments([$value->id()]);
$filters = $executable->getDisplay()->getOption('filters');;
$input = $this->extractExposedInput($value, $args, $filters);
$executable->setExposedInput($input);
// This is a workaround for the Taxonomy Term filter which requires a full
// exposed form to be sent OR the display being an attachment to just
// accept input values.
$executable->is_attachment = TRUE;
$executable->exposed_raw_input = $input;
if (!empty($definition['paged'])) {
// Set paging parameters.
$executable->setItemsPerPage($args['pageSize']);
$executable->setCurrentPage($args['page']);
}
if (isset($args['offset']) && !empty($args['offset'])) {
$executable->setOffset($args['offset']);
}
$result = $executable->render($definition['display']);
/** @var \Drupal\Core\Cache\CacheableMetadata $cache */
if ($cache = $result['cache']) {
$cache->setCacheContexts(
array_filter($cache->getCacheContexts(), function ($context) {
// Don't emit the url cache contexts.
return $context !== 'url' && strpos($context, 'url.') !== 0;
})
);
}
yield $result;
}
}
/**
* {@inheritdoc}
*/
protected function getCacheDependencies(array $result, $value, array $args, ResolveContext $context, ResolveInfo $info) {
return array_map(function ($item) {
return $item['cache'];
}, $result);
}
/**
* Retrieves the contextual filter argument from the parent value or args.
*
* @param $value
* The resolved parent value.
* @param $args
* The arguments provided to the field.
*
* @return array
* An array of arguments containing the contextual filter value from the
* parent or provided args if any.
*/
protected function extractContextualFilters($value, $args) {
$definition = $this->getPluginDefinition();
$arguments = [];
foreach ($definition['arguments_info'] as $argumentId => $argumentInfo) {
if (isset($args['contextualFilter'][$argumentId])) {
$arguments[$argumentInfo['index']] = $args['contextualFilter'][$argumentId];
}
elseif (
$value instanceof EntityInterface &&
$value->getEntityTypeId() === $argumentInfo['entity_type'] &&
(empty($argumentInfo['bundles']) ||
in_array($value->bundle(), $argumentInfo['bundles'], TRUE))
) {
$arguments[$argumentInfo['index']] = $value->id();
}
else {
$arguments[$argumentInfo['index']] = NULL;
}
}
return $arguments;
}
/**
* Retrieves sort and filter arguments from the provided field args.
*
* @param $value
* The resolved parent value.
* @param $args
* The array of arguments provided to the field.
* @param $filters
* The available filters for the configured view.
*
* @return array
* The array of sort and filter arguments to execute the view with.
*/
protected function extractExposedInput($value, $args, $filters) {
// Prepare arguments for use as exposed form input.
$input = array_filter([
// Sorting arguments.
'sort_by' => isset($args['sortBy']) ? $args['sortBy'] : NULL,
'sort_order' => isset($args['sortDirection']) ? $args['sortDirection'] : NULL,
]);
// If some filters are missing from the input, set them to an empty string
// explicitly. Otherwise views module generates "Undefined index" notice.
foreach ($filters as $filterKey => $filterRow) {
if (!isset($filterRow['expose']['identifier'])) {
continue;
}
$inputKey = $filterRow['expose']['identifier'];
if (!isset($args['filter'][$inputKey])) {
$input[$inputKey] = $filterRow['value'];
} else {
$input[$inputKey] = $args['filter'][$inputKey];
}
}
return $input;
}
}