Skip to content

Commit

Permalink
Alter graphql link field
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Jan 26, 2024
1 parent 2ca8ead commit 7a6fc0f
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 8 deletions.
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Drupal\sul_helper\Plugin\GraphQLCompose\FieldType;

use Drupal\Core\Field\FieldItemInterface;
use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\graphql_compose\Plugin\GraphQLCompose\FieldType\LinkItem;

/**
*
*/
class SulLinkItem extends LinkItem {

/**
* {@inheritdoc}
*/
public function resolveFieldItem(FieldItemInterface $item, FieldContext $context) {
$data = parent::resolveFieldItem($item, $context);
/** @var \Drupal\Core\TypedData\Plugin\DataType\Map $item_options */
$item_options = $item->get('options')->getValue();
if ($item_options && isset($item_options['attributes']['aria-label'])) {
$data['attributes']['ariaLabel'] = $item_options['attributes']['aria-label'];
}
return $data;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Drupal\sul_helper\Plugin\GraphQLCompose\SchemaType;

use Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeSchemaTypeBase;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;

/**
* {@inheritdoc}
*
* @GraphQLComposeSchemaType(
* id = "LinkAttributes"
* )
*/
class LinkAttributesType extends GraphQLComposeSchemaTypeBase {

/**
* {@inheritdoc}
*/
public function getTypes(): array {
$types = [];

$types[] = new ObjectType([
'name' => $this->getPluginId(),
'description' => (string) $this->t('Link Attributes.'),
'fields' => fn() => [
'ariaLabel' => [
'type' => Type::string(),
'description' => (string) $this->t('Aria Label'),
],
'ariaLabelledBy' => [
'type' => Type::string(),
'description' => (string) $this->t('Aria Labelled By'),
],
],
]);

return $types;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Drupal\sul_helper\Plugin\GraphQLCompose\SchemaType;

use Drupal\graphql_compose\Plugin\GraphQLCompose\SchemaType\LinkType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;

/**
*
*/
class SulLinkType extends LinkType {

/**
* {@inheritdoc}
*/
public function getTypes(): array {
$types = [];

$types[] = new ObjectType([
'name' => $this->getPluginId(),
'description' => (string) $this->t('A link.'),
'fields' => fn() => [
'title' => [
'type' => Type::string(),
'description' => (string) $this->t('The title of the link.'),
],
'url' => [
'type' => Type::string(),
'description' => (string) $this->t('The URL of the link.'),
],
'internal' => [
'type' => Type::nonNull(Type::boolean()),
'description' => (string) $this->t('Whether the link is internal to this website.'),
],
'attributes' => [
'type' => static::type('LinkAttributes'),
'description' => (string) $this->t('Link attributes'),
],
],
]);

return $types;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,17 @@ function sul_helper_preprocess_paragraph__stanford_spacer(&$variables) {
$variables['attributes']['class'][] = $paragraph->get('su_spacer_size')->getString();
}
}

/**
* Implements hook_graphql_compose_field_type_alter().
*/
function sul_helper_graphql_compose_field_type_alter(array &$field_types) {
$field_types['link']['class'] = 'Drupal\sul_helper\Plugin\GraphQLCompose\FieldType\SulLinkItem';
}

/**
* Implements hook_graphql_compose_graphql_type_alter().
*/
function sul_helper_graphql_compose_graphql_type_alter(array &$entity_types) {
$entity_types['Link']['class'] = 'Drupal\sul_helper\Plugin\GraphQLCompose\SchemaType\SulLinkType';
}

0 comments on commit 7a6fc0f

Please sign in to comment.