Skip to content
Snippets Groups Projects
Commit 6303369c authored by Al Munnings's avatar Al Munnings
Browse files

Issue #3445115: Allow adding interfaces per bundle

parent d57b8fbb
Branches
Tags
1 merge request!73Issue #3445115: Allow adding interfaces per bundle
Pipeline #164752 passed
......@@ -7,6 +7,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\graphql\GraphQL\Execution\FieldContext;
......@@ -130,10 +131,21 @@ function hook_graphql_compose_field_results_alter(array &$results, $entity, Grap
* Interfaces defined on entity type.
* @param \Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeEntityTypeInterface $plugin
* The current entity type being processed.
* @param string|null $bundle_id
* The current bundle being processed.
*/
function hook_graphql_compose_entity_interfaces_alter(array &$interfaces, GraphQLComposeEntityTypeInterface $plugin) {
if ($plugin->getBaseId() === 'block') {
$interfaces[] = 'TestBlocks';
function hook_graphql_compose_entity_interfaces_alter(array &$interfaces, GraphQLComposeEntityTypeInterface $plugin, ?string $bundle_id) {
if ($plugin->getEntityTypeId() === 'node') {
$interfaces[] = 'TestNodes';
}
if ($plugin->getEntityType()->entityClassImplements(FieldableEntityInterface::class)) {
$fields = \Drupal::service('entity_field.manager')
->getFieldDefinitions($plugin->getEntityTypeId(), $bundle_id);
if (isset($fields['field_tags'])) {
$interfaces[] = 'TaggableInterface';
}
}
}
......
......@@ -31,13 +31,8 @@ class CommentAvailable extends GraphQLComposeSchemaTypeBase {
foreach ($bundles as $bundle) {
$bundle_type_sdl = $bundle->getTypeSdl();
$entity_type = $base_type = $bundle->getEntity();
if ($bundle_of = $entity_type->getEntityType()->getBundleOf()) {
$base_type = $this->entityTypeManager->getDefinition($bundle_of);
}
$values[$bundle_type_sdl] = [
'value' => $base_type->id() . ':' . $bundle->getEntity()->id(),
'value' => $bundle->getEntityType()->id() . ':' . $bundle->getEntity()->id(),
'description' => $bundle->getDescription(),
];
}
......
......@@ -41,7 +41,7 @@ function graphql_compose_metatags_graphql_compose_entity_base_fields_alter(array
/**
* Implements hook_graphql_compose_entity_interfaces_alter().
*/
function graphql_compose_metatags_graphql_compose_entity_interfaces_alter(array &$interfaces, GraphQLComposeEntityTypeInterface $plugin) {
function graphql_compose_metatags_graphql_compose_entity_interfaces_alter(array &$interfaces, GraphQLComposeEntityTypeInterface $plugin, ?string $bundle_id) {
$base_fields = $plugin->getBaseFields();
// Add MetaTagInterface to enabled entity type.
......
......@@ -7,6 +7,7 @@ namespace Drupal\graphql_compose\Plugin\GraphQLCompose;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
......@@ -101,6 +102,13 @@ abstract class GraphQLComposeEntityTypeBase extends PluginBase implements GraphQ
return $this->getDerivativeId() ?: $this->getPluginId();
}
/**
* {@inheritdoc}
*/
public function getEntityType(): EntityTypeInterface {
return $this->entityTypeManager->getDefinition($this->getEntityTypeId());
}
/**
* {@inheritdoc}
*/
......@@ -113,7 +121,7 @@ abstract class GraphQLComposeEntityTypeBase extends PluginBase implements GraphQ
/**
* {@inheritdoc}
*/
public function getInterfaces(): array {
public function getInterfaces(?string $bundle_id = NULL): array {
$interfaces = $this->pluginDefinition['interfaces'] ?? [];
if ($this->gqlFieldTypeManager->getInterfaceFields($this->getEntityTypeId())) {
......@@ -123,6 +131,7 @@ abstract class GraphQLComposeEntityTypeBase extends PluginBase implements GraphQ
$this->moduleHandler->invokeAll('graphql_compose_entity_interfaces_alter', [
&$interfaces,
$this,
$bundle_id,
]);
return array_unique($interfaces);
......@@ -221,7 +230,7 @@ abstract class GraphQLComposeEntityTypeBase extends PluginBase implements GraphQ
$this->bundles = [];
$entity_type = $this->entityTypeManager->getDefinition($this->getEntityTypeId());
$entity_type = $this->getEntityType();
$bundle_info = $this->entityTypeBundleInfo->getBundleInfo($this->getEntityTypeId());
if ($storage_type = $entity_type->getBundleEntityType()) {
......@@ -250,10 +259,6 @@ abstract class GraphQLComposeEntityTypeBase extends PluginBase implements GraphQ
return;
}
// Check if the entity type has a type.
$entity_type = $this->entityTypeManager->getDefinition($this->getEntityTypeId());
$entity_type_has_bundles = $entity_type->getBundleEntityType();
// Register a bundle types into the schema.
foreach ($bundles as $bundle) {
$this->registerBundleTypes($bundle);
......@@ -309,8 +314,9 @@ abstract class GraphQLComposeEntityTypeBase extends PluginBase implements GraphQ
);
if ($this->isQueryLoadSimple() && $enabled_query_bundles) {
// Entities without bundles shouldn't return a union.
$query_type = $entity_type_has_bundles
$query_type = $this->getEntityType()->getBundleEntityType()
? $this->getUnionTypeSdl()
: $this->getTypeSdl();
......@@ -360,7 +366,7 @@ abstract class GraphQLComposeEntityTypeBase extends PluginBase implements GraphQ
'description' => $bundle->getDescription() ?: $this->getDescription(),
'interfaces' => fn() => array_map(
$this->gqlSchemaTypeManager->get(...),
$this->getInterfaces()
$this->getInterfaces($bundle->getEntity()->id())
),
'fields' => function () use ($fields) {
$result = [];
......@@ -474,7 +480,7 @@ abstract class GraphQLComposeEntityTypeBase extends PluginBase implements GraphQ
$enabled_query_bundle_ids = array_map(
fn(EntityTypeWrapper $bundle) => $bundle->getEntity()->id(),
$enabled_query_bundles
);
);
if ($this->isQueryLoadSimple() && $enabled_query_bundles) {
$registry->addFieldResolver(
......
......@@ -6,6 +6,7 @@ namespace Drupal\graphql_compose\Plugin\GraphQLCompose;
use Drupal\Component\Plugin\DerivativeInspectionInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
use Drupal\graphql_compose\Wrapper\EntityTypeWrapper;
......@@ -23,6 +24,14 @@ interface GraphQLComposeEntityTypeInterface extends PluginInspectionInterface, D
*/
public function getEntityTypeId(): string;
/**
* Get the entity type for this plugin.
*
* @return \Drupal\Core\Entity\EntityTypeInterface
* The entity type.
*/
public function getEntityType(): EntityTypeInterface;
/**
* Description of this entity type.
*
......@@ -34,10 +43,13 @@ interface GraphQLComposeEntityTypeInterface extends PluginInspectionInterface, D
/**
* Interfaces for this entity type.
*
* @param string|null $bundle_id
* The bundle to get interfaces for.
*
* @return string[]
* Interfaces for this entity type.
*/
public function getInterfaces(): array;
public function getInterfaces(?string $bundle_id = NULL): array;
/**
* Prefix for this entity type. Eg Paragraph.
......
......@@ -8,12 +8,13 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\graphql_compose\LanguageInflector;
use Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeEntityTypeInterface;
use Drupal\graphql_compose\Plugin\GraphQLComposeFieldTypeManager as PluginGraphQLComposeFieldTypeManager;
use Drupal\graphql_compose\Plugin\GraphQLComposeFieldTypeManager;
use Drupal\language\Entity\ContentLanguageSettings;
use function Symfony\Component\String\u;
......@@ -25,16 +26,12 @@ class EntityTypeWrapper {
use StringTranslationTrait;
/**
* The wrapped entity type plugin.
*
* @var mixed
* The wrapped entity (bundle).
*/
protected mixed $entity;
protected ConfigEntityInterface|EntityTypeInterface $entity;
/**
* The wrapped entity type plugin.
*
* @var \Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeEntityTypeInterface
*/
protected GraphQLComposeEntityTypeInterface $entityTypePlugin;
......@@ -58,7 +55,7 @@ class EntityTypeWrapper {
protected ConfigFactoryInterface $configFactory,
protected EntityFieldManagerInterface $entityFieldManager,
protected EntityTypeManagerInterface $entityTypeManager,
protected PluginGraphQLComposeFieldTypeManager $gqlFieldTypeManager,
protected GraphQLComposeFieldTypeManager $gqlFieldTypeManager,
protected LanguageInflector $inflector,
protected LanguageManagerInterface $languageManager,
) {}
......@@ -78,12 +75,12 @@ class EntityTypeWrapper {
}
/**
* Get the wrapped entity.
* Get the wrapped entity (bundle).
*
* @return mixed
* The wrapped entity.
* @return \Drupal\Core\Config\Entity\ConfigEntityInterface|\Drupal\Core\Entity\EntityTypeInterface
* The wrapped entity (bundle).
*/
public function getEntity(): mixed {
public function getEntity(): ConfigEntityInterface|EntityTypeInterface {
return $this->entity;
}
......@@ -111,6 +108,16 @@ class EntityTypeWrapper {
return $this->entityTypePlugin;
}
/**
* Get the wrapped entity type.
*
* @return \Drupal\Core\Entity\EntityTypeInterface
* The wrapped entity type.
*/
public function getEntityType(): EntityTypeInterface {
return $this->entityTypePlugin->getEntityType();
}
/**
* Type of this entity and bundle.
*
......@@ -169,6 +176,8 @@ class EntityTypeWrapper {
*
* @return string|null
* The description of the wrapped entity.
*
* @disregard P1009 Undefined type
*/
public function getDescription(): ?string {
return method_exists($this->entity, 'getDescription')
......@@ -217,17 +226,10 @@ class EntityTypeWrapper {
return FALSE;
}
$entity_type = $base_type = $this->entity;
if ($entity_type instanceof ConfigEntityInterface) {
if ($bundle_of = $entity_type->getEntityType()->getBundleOf()) {
$base_type = $this->entityTypeManager->getDefinition($bundle_of);
}
}
// Check if the entity bundle is translatable.
$config = ContentLanguageSettings::loadByEntityTypeBundle(
$base_type->id(),
$entity_type->id()
$this->getEntityType()->id(),
$this->entity->id()
);
return $config->isLanguageAlterable();
......@@ -245,14 +247,14 @@ class EntityTypeWrapper {
public function getSetting(string $setting): mixed {
$settings = $this->configFactory->get('graphql_compose.settings');
$entity_type = $base_type = $this->entity;
if ($entity_type instanceof ConfigEntityInterface) {
if ($bundle_of = $entity_type->getEntityType()->getBundleOf()) {
$base_type = $this->entityTypeManager->getDefinition($bundle_of);
}
}
$parts = [
'entity_config',
$this->getEntityType()->id(),
$this->entity->id(),
$setting,
];
return $settings->get('entity_config.' . $base_type->id() . '.' . $entity_type->id() . '.' . $setting) ?: NULL;
return $settings->get(implode('.', $parts)) ?: NULL;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment