From 0afb9bbc0abc94298bfa0ae3830a103127096eac Mon Sep 17 00:00:00 2001 From: Philipp Melab Date: Mon, 13 Nov 2023 10:02:08 +0000 Subject: [PATCH] refactor: use directive implementations instead of plugins --- .../sync/graphql.graphql_servers.main.yml | 1 + packages/drupal/custom/custom.services.yml | 9 ++- packages/drupal/custom/directives.graphql | 11 ++- packages/drupal/custom/src/ContentHub.php | 70 ++++++++++++++++++ .../Plugin/GraphQL/Directive/ContentHub.php | 53 -------------- .../GraphQL/Directive/WebformIdToUrl.php | 61 ---------------- packages/drupal/custom/src/Webform.php | 71 +++++++++++++++++++ packages/schema/src/schema.graphql | 4 +- 8 files changed, 161 insertions(+), 119 deletions(-) create mode 100644 packages/drupal/custom/src/ContentHub.php delete mode 100644 packages/drupal/custom/src/Plugin/GraphQL/Directive/ContentHub.php delete mode 100644 packages/drupal/custom/src/Plugin/GraphQL/Directive/WebformIdToUrl.php create mode 100644 packages/drupal/custom/src/Webform.php diff --git a/apps/cms/config/sync/graphql.graphql_servers.main.yml b/apps/cms/config/sync/graphql.graphql_servers.main.yml index ed80f37a8..1d9d21d43 100644 --- a/apps/cms/config/sync/graphql.graphql_servers.main.yml +++ b/apps/cms/config/sync/graphql.graphql_servers.main.yml @@ -15,6 +15,7 @@ query_complexity: null schema_configuration: directable: schema_definition: ../node_modules/@custom/schema/build/schema.graphql + autoload_registry: ../autoload.json extensions: silverback_campaign_urls: silverback_campaign_urls silverback_gatsby: silverback_gatsby diff --git a/packages/drupal/custom/custom.services.yml b/packages/drupal/custom/custom.services.yml index ca9f60be4..ddc26a4d3 100644 --- a/packages/drupal/custom/custom.services.yml +++ b/packages/drupal/custom/custom.services.yml @@ -2,4 +2,11 @@ services: custom.route_subscriber: class: Drupal\custom\Routing\RouteSubscriber tags: - - { name: event_subscriber } \ No newline at end of file + - { name: event_subscriber } + custom.content_hub: + class: Drupal\custom\ContentHub + arguments: ['@entity_type.manager'] + + custom.webform: + class: Drupal\custom\Webform + arguments: ['@renderer', '@entity_type.manager'] diff --git a/packages/drupal/custom/directives.graphql b/packages/drupal/custom/directives.graphql index 111d24925..3270606c1 100644 --- a/packages/drupal/custom/directives.graphql +++ b/packages/drupal/custom/directives.graphql @@ -1,2 +1,9 @@ -directive @contentHub on FIELD_DEFINITION -directive @webformIdToUrl on FIELD_DEFINITION +""" +implementation(drupal): custom.content_hub::query +""" +directive @contentHub(pagination: String, query: String) on FIELD_DEFINITION + +""" +implementation(drupal): custom.webform::url +""" +directive @webformIdToUrl(id: String!) on FIELD_DEFINITION diff --git a/packages/drupal/custom/src/ContentHub.php b/packages/drupal/custom/src/ContentHub.php new file mode 100644 index 000000000..d5f9bff9d --- /dev/null +++ b/packages/drupal/custom/src/ContentHub.php @@ -0,0 +1,70 @@ +entityTypeManager = $entityTypeManager; + } + + /** + * Query a list of pages. + * + * @param \Drupal\graphql_directives\DirectiveArguments $args + * The graphql argument bag. + * + * @return array{'total': int, 'items': \Drupal\node\NodeInterface[]} + * Result of the query. + */ + public function query(DirectiveArguments $args) : array { + // @todo Switch this to views. + $offset = $args->args['pagination']['offset']; + $limit = $args->args['pagination']['limit']; + $nodeStorage = $this->entityTypeManager->getStorage('node'); + + $countQuery = $nodeStorage->getQuery(); + $countQuery->condition('type', 'page'); + $countQuery->condition('status', 1); + if (!empty($args->args['query'])) { + $countQuery->condition('title', $args->args['query'], 'CONTAINS'); + } + $count = $countQuery->count()->accessCheck(TRUE)->execute(); + + $query = $nodeStorage->getQuery(); + $query->condition('type', 'page'); + $query->condition('status', 1); + if (!empty($args->args['query'])) { + $query->condition('title', $args->args['query'], 'CONTAINS'); + } + $pageIds = $query->range($offset, $limit) + ->sort('title', 'ASC') + ->accessCheck(TRUE) + ->execute(); + $posts = $pageIds ? $nodeStorage->loadMultiple($pageIds) : []; + return [ + 'total' => $count, + 'items' => $posts, + ]; + } + +} diff --git a/packages/drupal/custom/src/Plugin/GraphQL/Directive/ContentHub.php b/packages/drupal/custom/src/Plugin/GraphQL/Directive/ContentHub.php deleted file mode 100644 index e796f679d..000000000 --- a/packages/drupal/custom/src/Plugin/GraphQL/Directive/ContentHub.php +++ /dev/null @@ -1,53 +0,0 @@ -callback(function ($parent, $args) { - // TODO: Switch this to views. - $offset = $args['pagination']['offset']; - $limit = $args['pagination']['limit']; - - $countQuery = \Drupal::entityQuery('node'); - $countQuery->condition('type', 'page'); - $countQuery->condition('status', 1); - if (!empty($args['query'])) { - $countQuery->condition('title', $args['query'], 'CONTAINS'); - } - $count = $countQuery->count()->accessCheck(TRUE)->execute(); - - $query = \Drupal::entityQuery('node'); - $query->condition('type', 'page'); - $query->condition('status', 1); - if (!empty($args['query'])) { - $query->condition('title', $args['query'], 'CONTAINS'); - } - $pageIds = $query->range($offset, $limit) - ->sort('title', 'ASC') - ->accessCheck(TRUE) - ->execute(); - $posts = $pageIds ? Node::loadMultiple($pageIds) : []; - return [ - 'total' => $count, - 'items' => $posts, - ]; - }); - } - -} diff --git a/packages/drupal/custom/src/Plugin/GraphQL/Directive/WebformIdToUrl.php b/packages/drupal/custom/src/Plugin/GraphQL/Directive/WebformIdToUrl.php deleted file mode 100644 index 47a7b5adc..000000000 --- a/packages/drupal/custom/src/Plugin/GraphQL/Directive/WebformIdToUrl.php +++ /dev/null @@ -1,61 +0,0 @@ -callback(function ( - $webformId, - $args, - ResolveContext $context, - ResolveInfo $info, - FieldContext $field, - ) { - if (!$webformId) { - return NULL; - } - - /** @var \Drupal\Core\Render\RendererInterface $renderer */ - $renderer = \Drupal::service('renderer'); - $renderContext = new RenderContext(); - - $result = $renderer->executeInRenderContext( - $renderContext, - function () use ($field, $webformId) { - $webform = Webform::load($webformId); - if (!$webform || !$webform->isOpen()) { - return NULL; - } - $field->addCacheableDependency($webform); - return $webform->toUrl()->setAbsolute()->toString(); - }, - ); - - if (!$renderContext->isEmpty()) { - $field->addCacheableDependency($renderContext->pop()); - } - - return $result; - }); - } - -} diff --git a/packages/drupal/custom/src/Webform.php b/packages/drupal/custom/src/Webform.php new file mode 100644 index 000000000..6e80afe5d --- /dev/null +++ b/packages/drupal/custom/src/Webform.php @@ -0,0 +1,71 @@ +entityTypeManager = $entityTypeManager; + $this->renderer = $renderer; + } + + /** + * Generate the public url of a webform, based on its ID. + */ + public function url(DirectiveArguments $args) : ?string { + $webformId = $args->args['id']; + if (!$webformId) { + return NULL; + } + + $webFormStorage = $this->entityTypeManager->getStorage('webform'); + + /** @var \Drupal\Core\Render\RendererInterface $renderer */ + $renderContext = new RenderContext(); + + $result = $this->renderer->executeInRenderContext( + $renderContext, + function () use ($args, $webformId, $webFormStorage) { + $webform = $webFormStorage->load($webformId); + if (!$webform || !$webform->isOpen()) { + return NULL; + } + $args->context->addCacheableDependency($webform); + return $webform->toUrl()->setAbsolute()->toString(); + }, + ); + + if (!$renderContext->isEmpty()) { + $args->context->addCacheableDependency($renderContext->pop()); + } + + return $result; + } + +} diff --git a/packages/schema/src/schema.graphql b/packages/schema/src/schema.graphql index a731b03d4..0f5bacb15 100644 --- a/packages/schema/src/schema.graphql +++ b/packages/schema/src/schema.graphql @@ -95,7 +95,7 @@ type Hero { union PageContent @resolveEditorBlockType = BlockMarkup | BlockMedia | BlockForm type BlockForm @type(id: "custom/form") { - url: Url @resolveEditorBlockAttribute(key: "formId") @webformIdToUrl + url: Url @resolveEditorBlockAttribute(key: "formId") @webformIdToUrl(id: "$") } type BlockMarkup @type(id: "core/paragraph") { @@ -130,7 +130,7 @@ type Query { previewPage(id: ID!, rid: ID, locale: String!): Page @fetchEntity(type: "node", id: "$id", rid: "$rid", language: "$locale") contentHub(query: String, pagination: PaginationInput!): ContentHubResult! - @contentHub + @contentHub(query: "$query", pagination: "$pagination") } type MetaTag {