Skip to content

Commit

Permalink
Adjust ViewDeriver to properly use trait loaded in the ViewDeriverBase
Browse files Browse the repository at this point in the history
  • Loading branch information
das-peter committed Jan 7, 2020
1 parent 7f2fb62 commit 842c338
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 140 deletions.
139 changes: 0 additions & 139 deletions src/Plugin/Deriver/Fields/ViewDeriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,143 +56,4 @@ public function getDerivativeDefinitions($basePluginDefinition) {
return parent::getDerivativeDefinitions($basePluginDefinition);
}

/**
* Helper function to return the contextual filter argument if any exist.
*
* @param array $arguments
* The array of available arguments.
* @param $id
* The plugin derivative id.
*
* @return array
* The contextual filter argument if applicable.
*/
protected function getContextualArguments(array $arguments, $id) {
if (!empty($arguments)) {
return [
'contextualFilter' => [
'type' => StringHelper::camelCase($id, 'contextual', 'filter', 'input'),
],
];
}

return [];
}

/**
* Helper function to retrieve the sort arguments if any are exposed.
*
* @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display
* The display plugin.
* @param $id
* The plugin derivative id.
*
* @return array
* The sort arguments if any exposed sorts are available.
*/
protected function getSortArguments(DisplayPluginInterface $display, $id) {
$sorts = array_filter($display->getOption('sorts') ?: [], function ($sort) {
return $sort['exposed'];
});
return $sorts ? [
'sortDirection' => [
'type' => 'ViewSortDirection',
'default' => 'asc',
],
'sortBy' => [
'type' => StringHelper::camelCase($id, 'sort', 'by'),
],
] : [];
}

/**
* Helper function to return the filter argument if applicable.
*
* @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display
* The display plugin.
* @param $id
* The plugin derivative id.
*
* @return array
* The filter argument if any exposed filters are available.
*/
protected function getFilterArguments(DisplayPluginInterface $display, $id) {
$filters = array_filter($display->getOption('filters') ?: [], function($filter) {
return array_key_exists('exposed', $filter) && $filter['exposed'];
});

return !empty($filters) ? [
'filter' => [
'type' => $display->getGraphQLFilterInputName(),
],
] : [];
}

/**
* Helper function to retrieve the pager arguments if the display is paged.
*
* @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display
* The display plugin.
*
* @return array
* An array of pager arguments if the view display is paged.
*/
protected function getPagerArguments(DisplayPluginInterface $display) {
return $this->isPaged($display) ? [
'page' => ['type' => 'Int', 'default' => $this->getPagerOffset($display)],
'pageSize' => ['type' => 'Int', 'default' => $this->getPagerLimit($display)],
] : [];
}

/**
* Helper function to retrieve the types that the view can be attached to.
*
* @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display
* The display plugin.
* @param array $arguments
* An array containing information about the available arguments.
* @return array
* An array of additional types the view can be embedded in.
*/
protected function getTypes(DisplayPluginInterface $display, array $arguments) {
$types = ['Root'];

if (($entity_type = $display->getOption('entity_type'))) {
$types = array_merge($types, [StringHelper::camelCase($entity_type)]);

if (($bundles = $display->getOption('bundles'))) {
$types = array_merge($types, array_map(function ($bundle) use ($entity_type) {
return StringHelper::camelCase($entity_type, $bundle);
}, $bundles));
}
}

if (empty($arguments)) {
return $types;
}

foreach ($arguments as $argument) {
// Depending on whether bundles are known, we expose the view field
// either on the interface (e.g. Node) or on the type (e.g. NodePage)
// level. Here we specify types managed by other graphql_* modules,
// yet we don't define these modules as dependencies. If types are not
// in the schema, the resulting GraphQL field will be attached to
// nowhere, so it won't go into the schema.
if (empty($argument['bundles']) && empty($argument['entity_type'])) {
continue;
}

if (empty($argument['bundles'])) {
$types = array_merge($types, [StringHelper::camelCase($argument['entity_type'])]);
}
else {
$types = array_merge($types, array_map(function ($bundle) use ($argument) {
return StringHelper::camelCase($argument['entity_type'], $bundle);
}, array_keys($argument['bundles'])));
}
}

return $types;
}

}
12 changes: 11 additions & 1 deletion src/ViewDeriverHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,17 @@ protected function getPagerArguments(DisplayPluginInterface $display) {
* @return array
* An array of additional types the view can be embedded in.
*/
protected function getTypes(array $arguments, array $types = ['Root']) {
protected function getTypes(DisplayPluginInterface $display, array $arguments, array $types = ['Root']) {

if (($entity_type = $display->getOption('entity_type'))) {
$types = array_merge($types, [StringHelper::camelCase($entity_type)]);

if (($bundles = $display->getOption('bundles'))) {
$types = array_merge($types, array_map(function ($bundle) use ($entity_type) {
return StringHelper::camelCase($entity_type, $bundle);
}, $bundles));
}
}

if (empty($arguments)) {
return $types;
Expand Down

0 comments on commit 842c338

Please sign in to comment.