diff --git a/src/Plugin/GraphQLCompose/FieldType/ViewsReferenceItem.php b/src/Plugin/GraphQLCompose/FieldType/ViewsReferenceItem.php new file mode 100644 index 0000000..8696ca5 --- /dev/null +++ b/src/Plugin/GraphQLCompose/FieldType/ViewsReferenceItem.php @@ -0,0 +1,65 @@ +target_id) || empty($item->display_id)) { + return NULL; + } + + $view = Views::getView($item->target_id); + $view->setDisplay($item->display_id); + + if (!$view || !$view->access($item->display_id)) { + return NULL; + } + + $context->addCacheableDependency($view); + + /** @var \Drupal\graphql_compose_views\Plugin\views\display\GraphQL $display */ + $display = $view->getDisplay(); + $options = unserialize($item->data) ?? []; + $args = empty($options['argument']) ? NULL : explode('/', $options['argument'] ?? ''); + $size = is_numeric($options['limit']) ? (int) $options['limit'] : NULL; + $offset = is_numeric($options['offset']) ? (int) $options['offset'] : NULL; + + return [ + 'view' => $item->target_id, + 'display' => $item->display_id, + 'contextualFilter' => $args, + 'pageSize' => $size, + 'sort' => isset($options['sort']['field']) ? $options['sort'] : NULL, + 'pager' => $options['pager'] ?? NULL, + 'offset' => $offset, + 'query' => $display instanceof GraphQL ? $display->getGraphQlQueryName() : NULL, + ]; + } + +} diff --git a/src/Plugin/GraphQLCompose/SchemaType/ViewsReferenceSort.php b/src/Plugin/GraphQLCompose/SchemaType/ViewsReferenceSort.php new file mode 100644 index 0000000..3e73ac1 --- /dev/null +++ b/src/Plugin/GraphQLCompose/SchemaType/ViewsReferenceSort.php @@ -0,0 +1,46 @@ + $this->getPluginId(), + 'description' => (string) $this->t('A reference to an embedded view'), + 'fields' => fn() => [ + 'field' => [ + 'type' => Type::nonNull(Type::string()), + 'description' => (string) $this->t('The machine name of the view.'), + ], + 'direction' => [ + 'type' => Type::nonNull(Type::string()), + 'description' => (string) $this->t('The machine name of the display.'), + ], + ], + ]); + + return $types; + } + +} diff --git a/src/Plugin/GraphQLCompose/SchemaType/ViewsReferenceType.php b/src/Plugin/GraphQLCompose/SchemaType/ViewsReferenceType.php new file mode 100644 index 0000000..7587ab1 --- /dev/null +++ b/src/Plugin/GraphQLCompose/SchemaType/ViewsReferenceType.php @@ -0,0 +1,72 @@ + $this->getPluginId(), + 'description' => (string) $this->t('A reference to an embedded view'), + 'fields' => fn() => [ + 'view' => [ + 'type' => Type::nonNull(Type::string()), + 'description' => (string) $this->t('The machine name of the view.'), + ], + 'display' => [ + 'type' => Type::nonNull(Type::string()), + 'description' => (string) $this->t('The machine name of the display.'), + ], + 'contextualFilter' => [ + 'type' => Type::listOf(Type::nonNull(Type::string())), + 'description' => (string) $this->t('The contextual filter values used.'), + ], + 'pageSize' => [ + 'type' => Type::int(), + 'description' => (string) $this->t('How many results per page.'), + ], + + 'sort' => [ + 'type' => static::type('ViewsReferenceSort'), + 'description' => (string) $this->t('How many results per page.'), + ], + 'pager' => [ + 'type' => Type::string(), + 'description' => (string) $this->t('How many results per page.'), + ], + 'offset' => [ + 'type' => Type::int(), + 'description' => (string) $this->t('How many results per page.'), + ], + + 'query' => [ + 'type' => Type::string(), + 'description' => (string) $this->t('The name of the query used to fetch the data, if the view is a GraphQL display.'), + ], + ], + ]); + + return $types; + } + +} diff --git a/src/Plugin/ViewsReferenceSetting/ViewsReferenceSort.php b/src/Plugin/ViewsReferenceSetting/ViewsReferenceSort.php new file mode 100644 index 0000000..274b314 --- /dev/null +++ b/src/Plugin/ViewsReferenceSetting/ViewsReferenceSort.php @@ -0,0 +1,110 @@ +get('entity_type.manager') + ); + } + + /** + * @param array $configuration + * Plugin configuration. + * @param $plugin_id + * Plugin ID. + * @param $plugin_definition + * Plugin definition. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * Entity type manager service. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, protected EntityTypeManagerInterface $entityTypeManager) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + } + + /** + * {@inheritdoc} + */ + public function alterFormField(array &$form_field) { + $view = $this->configuration['view_name']; + $display_id = $this->configuration['display_id']; + if (!$view || !$display_id) { + return; + } + /** @var \Drupal\views\Entity\View $view */ + $view = $this->entityTypeManager->getStorage('view')->load($view); + + $display = $view->getDisplay($display_id); + $default = $view->getDisplay('default'); + $sorts = $display['display_options']['sorts'] ?? $default['display_options']['sorts']; + $exposed_sorts = array_filter($sorts, fn($sort) => $sort['exposed']); + $sort_options = []; + foreach ($exposed_sorts as $sort) { + $sort_options[$sort['id']] = $sort['expose']['label']; + } + + if (empty($sort_options)) { + $form_field['#access'] = FALSE; + } + + $form_field['#type'] = 'container'; + $form_field['field'] = [ + '#type' => 'select', + '#title' => $this->t('Field'), + '#options' => $sort_options, + '#empty_option' => $this->t('Default Sort'), + '#default_value' => $form_field['#default_value']['field'] ?? NULL, + ]; + + $form_field['direction'] = [ + '#type' => 'select', + '#title' => $this->t('Sort Direction'), + '#options' => [ + 'ASC' => $this->t('Ascending'), + 'DESC' => $this->t('Descending'), + ], + '#default_value' => $form_field['#default_value']['direction'] ?? 'ASC', + ]; + unset($form_field['#default_value']); + } + + /** + * {@inheritdoc} + */ + public function alterView(ViewExecutable $view, $value) { + if (!empty($value)) { + $view->setExposedInput([ + 'sort_by' => $value['field'], + 'sort_order' => $value['direction'], + ]); + } + } + +}