Skip to content

Commit eaac7ac

Browse files
LennardWesterveldfubhy
authored andcommitted
feat(viewsreference): added support for viewsreference. (#22)
1 parent d2d0f5c commit eaac7ac

File tree

11 files changed

+658
-338
lines changed

11 files changed

+658
-338
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "drupal/graphql-views",
2+
"name": "drupal/graphql_views",
33
"type": "drupal-module",
44
"description": "Exposes your Drupal Views data model through a GraphQL schema.",
55
"homepage": "http://drupal.org/project/graphql_views",

src/Plugin/Deriver/Fields/ViewDeriver.php

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace Drupal\graphql_views\Plugin\Deriver\Fields;
44

55
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
6-
use Drupal\graphql\Utility\StringHelper;
76
use Drupal\graphql_views\Plugin\Deriver\ViewDeriverBase;
8-
use Drupal\views\Plugin\views\display\DisplayPluginInterface;
97
use Drupal\views\Views;
108

119
/**
@@ -56,131 +54,4 @@ public function getDerivativeDefinitions($basePluginDefinition) {
5654
return parent::getDerivativeDefinitions($basePluginDefinition);
5755
}
5856

59-
/**
60-
* Helper function to return the contextual filter argument if any exist.
61-
*
62-
* @param array $arguments
63-
* The array of available arguments.
64-
* @param $id
65-
* The plugin derivative id.
66-
*
67-
* @return array
68-
* The contextual filter argument if applicable.
69-
*/
70-
protected function getContextualArguments(array $arguments, $id) {
71-
if (!empty($arguments)) {
72-
return [
73-
'contextualFilter' => [
74-
'type' => StringHelper::camelCase($id, 'contextual', 'filter', 'input'),
75-
],
76-
];
77-
}
78-
79-
return [];
80-
}
81-
82-
/**
83-
* Helper function to retrieve the sort arguments if any are exposed.
84-
*
85-
* @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display
86-
* The display plugin.
87-
* @param $id
88-
* The plugin derivative id.
89-
*
90-
* @return array
91-
* The sort arguments if any exposed sorts are available.
92-
*/
93-
protected function getSortArguments(DisplayPluginInterface $display, $id) {
94-
$sorts = array_filter($display->getOption('sorts') ?: [], function ($sort) {
95-
return $sort['exposed'];
96-
});
97-
return $sorts ? [
98-
'sortDirection' => [
99-
'type' => 'ViewSortDirection',
100-
'default' => 'asc',
101-
],
102-
'sortBy' => [
103-
'type' => StringHelper::camelCase($id, 'sort', 'by'),
104-
],
105-
] : [];
106-
}
107-
108-
/**
109-
* Helper function to return the filter argument if applicable.
110-
*
111-
* @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display
112-
* The display plugin.
113-
* @param $id
114-
* The plugin derivative id.
115-
*
116-
* @return array
117-
* The filter argument if any exposed filters are available.
118-
*/
119-
protected function getFilterArguments(DisplayPluginInterface $display, $id) {
120-
$filters = array_filter($display->getOption('filters') ?: [], function($filter) {
121-
return array_key_exists('exposed', $filter) && $filter['exposed'];
122-
});
123-
124-
return !empty($filters) ? [
125-
'filter' => [
126-
'type' => $display->getGraphQLFilterInputName(),
127-
],
128-
] : [];
129-
}
130-
131-
/**
132-
* Helper function to retrieve the pager arguments if the display is paged.
133-
*
134-
* @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display
135-
* The display plugin.
136-
*
137-
* @return array
138-
* An array of pager arguments if the view display is paged.
139-
*/
140-
protected function getPagerArguments(DisplayPluginInterface $display) {
141-
return $this->isPaged($display) ? [
142-
'page' => ['type' => 'Int', 'default' => $this->getPagerOffset($display)],
143-
'pageSize' => ['type' => 'Int', 'default' => $this->getPagerLimit($display)],
144-
] : [];
145-
}
146-
147-
/**
148-
* Helper function to retrieve the types that the view can be attached to.
149-
*
150-
* @param array $arguments
151-
* An array containing information about the available arguments.
152-
* @return array
153-
* An array of additional types the view can be embedded in.
154-
*/
155-
protected function getTypes(array $arguments) {
156-
$types = ['Root'];
157-
158-
if (empty($arguments)) {
159-
return $types;
160-
}
161-
162-
foreach ($arguments as $argument) {
163-
// Depending on whether bundles are known, we expose the view field
164-
// either on the interface (e.g. Node) or on the type (e.g. NodePage)
165-
// level. Here we specify types managed by other graphql_* modules,
166-
// yet we don't define these modules as dependencies. If types are not
167-
// in the schema, the resulting GraphQL field will be attached to
168-
// nowhere, so it won't go into the schema.
169-
if (empty($argument['bundles']) && empty($argument['entity_type'])) {
170-
continue;
171-
}
172-
173-
if (empty($argument['bundles'])) {
174-
$types = array_merge($types, [StringHelper::camelCase($argument['entity_type'])]);
175-
}
176-
else {
177-
$types = array_merge($types, array_map(function ($bundle) use ($argument) {
178-
return StringHelper::camelCase($argument['entity_type'], $bundle);
179-
}, array_keys($argument['bundles'])));
180-
}
181-
}
182-
183-
return $types;
184-
}
185-
18657
}

src/Plugin/Deriver/ViewDeriverBase.php

Lines changed: 7 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Drupal\graphql_views\Plugin\views\row\GraphQLEntityRow;
1111
use Drupal\graphql_views\Plugin\views\row\GraphQLFieldRow;
1212
use Drupal\graphql\Utility\StringHelper;
13+
use Drupal\graphql_views\ViewDeriverHelperTrait;
1314
use Drupal\views\Plugin\views\display\DisplayPluginInterface;
1415
use Drupal\views\ViewEntityInterface;
1516
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -18,6 +19,9 @@
1819
* Base class for graphql view derivers.
1920
*/
2021
abstract class ViewDeriverBase extends DeriverBase implements ContainerDeriverInterface {
22+
use ViewDeriverHelperTrait {
23+
getRowResolveType as private traitGetRowResolveType;
24+
}
2125
/**
2226
* The entity type manager.
2327
*
@@ -65,52 +69,6 @@ public function __construct(
6569
$this->entityTypeManager = $entityTypeManager;
6670
}
6771

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-
11472
/**
11573
* Retrieves the entity type id of an entity by its base or data table.
11674
*
@@ -142,172 +100,14 @@ protected function getEntityTypeByTable($table) {
142100
*
143101
* @param \Drupal\views\ViewEntityInterface $view
144102
* The view entity.
145-
* @param $displayId
146-
* The id of the current display.
103+
* @param string $displayId
104+
* Interface plugin manager.
147105
*
148106
* @return null|string
149107
* The name of the type or NULL if the type could not be derived.
150108
*/
151109
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);
206111
}
207112

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-
}
313113
}

0 commit comments

Comments
 (0)