|
10 | 10 | use Drupal\graphql_views\Plugin\views\row\GraphQLEntityRow; |
11 | 11 | use Drupal\graphql_views\Plugin\views\row\GraphQLFieldRow; |
12 | 12 | use Drupal\graphql\Utility\StringHelper; |
| 13 | +use Drupal\graphql_views\ViewDeriverHelperTrait; |
13 | 14 | use Drupal\views\Plugin\views\display\DisplayPluginInterface; |
14 | 15 | use Drupal\views\ViewEntityInterface; |
15 | 16 | use Symfony\Component\DependencyInjection\ContainerInterface; |
|
18 | 19 | * Base class for graphql view derivers. |
19 | 20 | */ |
20 | 21 | abstract class ViewDeriverBase extends DeriverBase implements ContainerDeriverInterface { |
| 22 | + use ViewDeriverHelperTrait { |
| 23 | + getRowResolveType as private traitGetRowResolveType; |
| 24 | + } |
21 | 25 | /** |
22 | 26 | * The entity type manager. |
23 | 27 | * |
@@ -65,52 +69,6 @@ public function __construct( |
65 | 69 | $this->entityTypeManager = $entityTypeManager; |
66 | 70 | } |
67 | 71 |
|
68 | | - /** |
69 | | - * Check if a pager is configured. |
70 | | - * |
71 | | - * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
72 | | - * The display configuration. |
73 | | - * |
74 | | - * @return bool |
75 | | - * Flag indicating if the view is configured with a pager. |
76 | | - */ |
77 | | - protected function isPaged(DisplayPluginInterface $display) { |
78 | | - $pagerOptions = $display->getOption('pager'); |
79 | | - return isset($pagerOptions['type']) && in_array($pagerOptions['type'], ['full', 'mini']); |
80 | | - } |
81 | | - |
82 | | - /** |
83 | | - * Get the configured default limit. |
84 | | - * |
85 | | - * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
86 | | - * The display configuration. |
87 | | - * |
88 | | - * @return int |
89 | | - * The default limit. |
90 | | - */ |
91 | | - protected function getPagerLimit(DisplayPluginInterface $display) { |
92 | | - $pagerOptions = $display->getOption('pager'); |
93 | | - return NestedArray::getValue($pagerOptions, [ |
94 | | - 'options', 'items_per_page', |
95 | | - ]) ?: 0; |
96 | | - } |
97 | | - |
98 | | - /** |
99 | | - * Get the configured default offset. |
100 | | - * |
101 | | - * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
102 | | - * The display configuration. |
103 | | - * |
104 | | - * @return int |
105 | | - * The default offset. |
106 | | - */ |
107 | | - protected function getPagerOffset(DisplayPluginInterface $display) { |
108 | | - $pagerOptions = $display->getOption('pager'); |
109 | | - return NestedArray::getValue($pagerOptions, [ |
110 | | - 'options', 'offset', |
111 | | - ]) ?: 0; |
112 | | - } |
113 | | - |
114 | 72 | /** |
115 | 73 | * Retrieves the entity type id of an entity by its base or data table. |
116 | 74 | * |
@@ -142,172 +100,14 @@ protected function getEntityTypeByTable($table) { |
142 | 100 | * |
143 | 101 | * @param \Drupal\views\ViewEntityInterface $view |
144 | 102 | * The view entity. |
145 | | - * @param $displayId |
146 | | - * The id of the current display. |
| 103 | + * @param string $displayId |
| 104 | + * Interface plugin manager. |
147 | 105 | * |
148 | 106 | * @return null|string |
149 | 107 | * The name of the type or NULL if the type could not be derived. |
150 | 108 | */ |
151 | 109 | protected function getRowResolveType(ViewEntityInterface $view, $displayId) { |
152 | | - /** @var \Drupal\graphql_views\Plugin\views\display\GraphQL $display */ |
153 | | - $display = $this->getViewDisplay($view, $displayId); |
154 | | - $rowPlugin = $display->getPlugin('row'); |
155 | | - |
156 | | - if ($rowPlugin instanceof GraphQLFieldRow) { |
157 | | - return StringHelper::camelCase($display->getGraphQLRowName()); |
158 | | - } |
159 | | - |
160 | | - if ($rowPlugin instanceof GraphQLEntityRow) { |
161 | | - $executable = $view->getExecutable(); |
162 | | - $executable->setDisplay($displayId); |
163 | | - |
164 | | - if ($entityType = $executable->getBaseEntityType()) { |
165 | | - $typeName = $entityType->id(); |
166 | | - $typeNameCamel = StringHelper::camelCase($typeName); |
167 | | - if ($this->interfaceExists($typeNameCamel)) { |
168 | | - $filters = $executable->getDisplay()->getOption('filters'); |
169 | | - $dataTable = $entityType->getDataTable(); |
170 | | - $bundleKey = $entityType->getKey('bundle'); |
171 | | - |
172 | | - foreach ($filters as $filter) { |
173 | | - $isBundleFilter = $filter['table'] == $dataTable && $filter['field'] == $bundleKey; |
174 | | - $isSingleValued = is_array($filter['value']) && count($filter['value']) == 1; |
175 | | - $isExposed = isset($filter['exposed']) && $filter['exposed']; |
176 | | - if ($isBundleFilter && $isSingleValued && !$isExposed) { |
177 | | - $bundle = reset($filter['value']); |
178 | | - $typeName .= "_$bundle"; |
179 | | - break; |
180 | | - } |
181 | | - } |
182 | | - |
183 | | - return StringHelper::camelCase($typeName); |
184 | | - } |
185 | | - } |
186 | | - |
187 | | - return 'Entity'; |
188 | | - } |
189 | | - |
190 | | - return NULL; |
191 | | - } |
192 | | - |
193 | | - /** |
194 | | - * Check if a certain interface exists. |
195 | | - * |
196 | | - * @param string $interface |
197 | | - * The GraphQL interface name. |
198 | | - * |
199 | | - * @return bool |
200 | | - * Boolean flag indicating if the interface exists. |
201 | | - */ |
202 | | - protected function interfaceExists($interface) { |
203 | | - return (bool) array_filter($this->interfacePluginManager->getDefinitions(), function($definition) use ($interface) { |
204 | | - return $definition['name'] === $interface; |
205 | | - }); |
| 110 | + return $this->traitGetRowResolveType($view, $displayId, $this->interfacePluginManager); |
206 | 111 | } |
207 | 112 |
|
208 | | - /** |
209 | | - * Returns a view display object. |
210 | | - * |
211 | | - * @param \Drupal\views\ViewEntityInterface $view |
212 | | - * The view object. |
213 | | - * @param string $displayId |
214 | | - * The display ID to use. |
215 | | - * |
216 | | - * @return \Drupal\views\Plugin\views\display\DisplayPluginInterface |
217 | | - * The view display object. |
218 | | - */ |
219 | | - protected function getViewDisplay(ViewEntityInterface $view, $displayId) { |
220 | | - $viewExecutable = $view->getExecutable(); |
221 | | - $viewExecutable->setDisplay($displayId); |
222 | | - return $viewExecutable->getDisplay(); |
223 | | - } |
224 | | - |
225 | | - /** |
226 | | - * Returns a view style object. |
227 | | - * |
228 | | - * @param \Drupal\views\ViewEntityInterface $view |
229 | | - * The view object. |
230 | | - * @param string $displayId |
231 | | - * The display ID to use. |
232 | | - * |
233 | | - * @return \Drupal\views\Plugin\views\style\StylePluginBase |
234 | | - * The view style object. |
235 | | - */ |
236 | | - protected function getViewStyle(ViewEntityInterface $view, $displayId) { |
237 | | - $viewExecutable = $view->getExecutable(); |
238 | | - $viewExecutable->setDisplay($displayId); |
239 | | - return $viewExecutable->getStyle(); |
240 | | - } |
241 | | - |
242 | | - /** |
243 | | - * Returns cache metadata plugin definitions. |
244 | | - * |
245 | | - * @param \Drupal\views\ViewEntityInterface $view |
246 | | - * The view object. |
247 | | - * |
248 | | - * @return array |
249 | | - * The cache metadata definitions for the plugin definition. |
250 | | - */ |
251 | | - protected function getCacheMetadataDefinition(ViewEntityInterface $view, DisplayPluginInterface $display) { |
252 | | - $metadata = $display->getCacheMetadata() |
253 | | - ->addCacheTags($view->getCacheTags()) |
254 | | - ->addCacheContexts($view->getCacheContexts()) |
255 | | - ->mergeCacheMaxAge($view->getCacheMaxAge()); |
256 | | - |
257 | | - return [ |
258 | | - 'schema_cache_tags' => $metadata->getCacheTags(), |
259 | | - 'schema_cache_max_age' => $metadata->getCacheMaxAge(), |
260 | | - 'response_cache_contexts' => array_filter($metadata->getCacheContexts(), function ($context) { |
261 | | - // Don't emit the url cache contexts. |
262 | | - return $context !== 'url' && strpos($context, 'url.') !== 0; |
263 | | - }), |
264 | | - ]; |
265 | | - } |
266 | | - |
267 | | - /** |
268 | | - * Returns information about view arguments (contextual filters). |
269 | | - * |
270 | | - * @param array $viewArguments |
271 | | - * The "arguments" option of a view display. |
272 | | - * |
273 | | - * @return array |
274 | | - * Arguments information keyed by the argument ID. Subsequent array keys: |
275 | | - * - index: argument index. |
276 | | - * - entity_type: target entity type. |
277 | | - * - bundles: target bundles (can be empty). |
278 | | - */ |
279 | | - protected function getArgumentsInfo(array $viewArguments) { |
280 | | - $argumentsInfo = []; |
281 | | - |
282 | | - $index = 0; |
283 | | - foreach ($viewArguments as $argumentId => $argument) { |
284 | | - $info = [ |
285 | | - 'index' => $index, |
286 | | - 'entity_type' => NULL, |
287 | | - 'bundles' => [], |
288 | | - ]; |
289 | | - |
290 | | - if (isset($argument['entity_type']) && isset($argument['entity_field'])) { |
291 | | - $entityType = $this->entityTypeManager->getDefinition($argument['entity_type']); |
292 | | - if ($entityType) { |
293 | | - $idField = $entityType->getKey('id'); |
294 | | - if ($idField === $argument['entity_field']) { |
295 | | - $info['entity_type'] = $argument['entity_type']; |
296 | | - if ( |
297 | | - $argument['specify_validation'] && |
298 | | - strpos($argument['validate']['type'], 'entity:') === 0 && |
299 | | - !empty($argument['validate_options']['bundles']) |
300 | | - ) { |
301 | | - $info['bundles'] = $argument['validate_options']['bundles']; |
302 | | - } |
303 | | - } |
304 | | - } |
305 | | - } |
306 | | - |
307 | | - $argumentsInfo[$argumentId] = $info; |
308 | | - $index++; |
309 | | - } |
310 | | - |
311 | | - return $argumentsInfo; |
312 | | - } |
313 | 113 | } |
0 commit comments