-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use directive implementations instead of plugins
- Loading branch information
Showing
8 changed files
with
161 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Drupal\custom; | ||
|
||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\graphql_directives\DirectiveArguments; | ||
|
||
/** | ||
* Service to query content listings. | ||
*/ | ||
class ContentHub { | ||
|
||
/** | ||
* The entity type manager, to query and load pages. | ||
* | ||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
*/ | ||
protected EntityTypeManagerInterface $entityTypeManager; | ||
|
||
/** | ||
* ContentHub constructor. | ||
* | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager | ||
* The entity type manager, to query and load pages. | ||
*/ | ||
public function __construct(EntityTypeManagerInterface $entityTypeManager) { | ||
$this->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, | ||
]; | ||
} | ||
|
||
} |
53 changes: 0 additions & 53 deletions
53
packages/drupal/custom/src/Plugin/GraphQL/Directive/ContentHub.php
This file was deleted.
Oops, something went wrong.
61 changes: 0 additions & 61 deletions
61
packages/drupal/custom/src/Plugin/GraphQL/Directive/WebformIdToUrl.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Drupal\custom; | ||
|
||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\Render\RenderContext; | ||
use Drupal\Core\Render\RendererInterface; | ||
use Drupal\graphql_directives\DirectiveArguments; | ||
|
||
/** | ||
* Helper service for managing webforms with graphql. | ||
*/ | ||
class Webform { | ||
|
||
/** | ||
* The renderer for collecting cache information. | ||
*/ | ||
protected RendererInterface $renderer; | ||
|
||
/** | ||
* The entity type manager, to query and load pages. | ||
* | ||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
*/ | ||
protected EntityTypeManagerInterface $entityTypeManager; | ||
|
||
/** | ||
* Webform constructor. | ||
*/ | ||
public function __construct( | ||
RendererInterface $renderer, | ||
EntityTypeManagerInterface $entityTypeManager | ||
) { | ||
$this->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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters