Skip to content

Commit bbe602a

Browse files
authored
Entity definition data producers. (#860)
1 parent f3d412e commit bbe602a

File tree

16 files changed

+986
-0
lines changed

16 files changed

+986
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition;
4+
5+
use Drupal\Core\Entity\EntityTypeInterface;
6+
use Drupal\Core\Entity\EntityTypeManager;
7+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8+
use Drupal\graphql\GraphQL\Execution\FieldContext;
9+
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
10+
use Symfony\Component\DependencyInjection\ContainerInterface;
11+
12+
/**
13+
* Gets entity definition for a given entity type.
14+
*
15+
* @DataProducer(
16+
* id = "entity_definition",
17+
* name = @Translation("Entity definition"),
18+
* description = @Translation("Return entity definitions for given entity type."),
19+
* consumes = {
20+
* "entity_type" = @ContextDefinition("string",
21+
* label = @Translation("Entity type")
22+
* ),
23+
* "bundle" = @ContextDefinition("string",
24+
* label = @Translation("Bundle"),
25+
* required = FALSE
26+
* ),
27+
* "field_types" = @ContextDefinition("FieldTypes",
28+
* label = @Translation("Field types"),
29+
* default = "ALL",
30+
* required = FALSE
31+
* )
32+
* },
33+
* produces = @ContextDefinition("any",
34+
* label = @Translation("Entity definition")
35+
* )
36+
* )
37+
*/
38+
class EntityDefinition extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
39+
40+
/**
41+
* The entity type manager service.
42+
*
43+
* @var \Drupal\Core\Entity\EntityTypeManager
44+
*/
45+
protected $entityTypeManager;
46+
47+
/**
48+
* {@inheritdoc}
49+
*
50+
* @codeCoverageIgnore
51+
*/
52+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
53+
return new static(
54+
$configuration,
55+
$plugin_id,
56+
$plugin_definition,
57+
$container->get('entity_type.manager')
58+
);
59+
}
60+
61+
/**
62+
* EntityLoad constructor.
63+
*
64+
* @param array $configuration
65+
* The plugin configuration array.
66+
* @param string $plugin_id
67+
* The plugin id.
68+
* @param array $plugin_definition
69+
* The plugin definition array.
70+
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
71+
* The entity type manager service.
72+
*
73+
* @codeCoverageIgnore
74+
*/
75+
public function __construct(
76+
array $configuration,
77+
string $plugin_id,
78+
array $plugin_definition,
79+
EntityTypeManager $entity_type_manager
80+
) {
81+
parent::__construct($configuration, $plugin_id, $plugin_definition);
82+
$this->entityTypeManager = $entity_type_manager;
83+
}
84+
85+
/**
86+
* Resolves entity definition for a given entity type.
87+
*
88+
* @param string $entity_type
89+
* The entity type.
90+
* @param string|null $bundle
91+
* Optional. The entity bundle which are stored as a context for upcoming
92+
* data producers deeper in hierarchy.
93+
* @param string|null $field_types
94+
* Optional. The field types to retrieve (base fields, configurable fields,
95+
* or both) which are stored as a context for upcoming data producers deeper
96+
* in hierarchy.
97+
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $field_context
98+
* Field context.
99+
*
100+
* @return \Drupal\Core\Entity\EntityTypeInterface
101+
* The entity definition.
102+
*/
103+
public function resolve(string $entity_type,
104+
?string $bundle = NULL,
105+
?string $field_types = NULL,
106+
FieldContext $field_context
107+
): EntityTypeInterface {
108+
if ($bundle) {
109+
$bundle_info = \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type);
110+
if (isset($bundle_info[$bundle])) {
111+
$bundle_context = $bundle_info[$bundle];
112+
$bundle_context['key'] = $bundle;
113+
$field_context->setContextValue('bundle', $bundle_context);
114+
}
115+
}
116+
117+
if ($field_types) {
118+
$field_context->setContextValue('field_types', $field_types);
119+
}
120+
121+
return $this->entityTypeManager->getDefinition($entity_type);
122+
}
123+
124+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
namespace Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition;
4+
5+
use Drupal\Core\Entity\ContentEntityType;
6+
use Drupal\Core\Entity\EntityTypeInterface;
7+
use Drupal\Core\Entity\EntityTypeManager;
8+
use Drupal\Core\Field\BaseFieldDefinition;
9+
use Drupal\Core\Field\Entity\BaseFieldOverride;
10+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11+
use Drupal\field\Entity\FieldConfig;
12+
use Drupal\graphql\GraphQL\Execution\FieldContext;
13+
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
14+
use Symfony\Component\DependencyInjection\ContainerInterface;
15+
16+
/**
17+
* Retrieve the list of fields from a given entity definition.
18+
*
19+
* @DataProducer(
20+
* id = "entity_definition_fields",
21+
* name = @Translation("Entity definition fields"),
22+
* description = @Translation("Return entity definition fields."),
23+
* consumes = {
24+
* "entity_definition" = @ContextDefinition("any",
25+
* label = @Translation("Entity definition")
26+
* ),
27+
* "bundle_context" = @ContextDefinition("any",
28+
* label = @Translation("Bundle context"),
29+
* required = FALSE,
30+
* ),
31+
* "field_types_context" = @ContextDefinition("any",
32+
* label = @Translation("Field types context"),
33+
* required = FALSE,
34+
* )
35+
* },
36+
* produces = @ContextDefinition("any",
37+
* label = @Translation("Entity definition field")
38+
* )
39+
* )
40+
*/
41+
class Fields extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
42+
43+
/**
44+
* The entity type manager service.
45+
*
46+
* @var \Drupal\Core\Entity\EntityTypeManager
47+
*/
48+
protected $entityTypeManager;
49+
50+
/**
51+
* {@inheritdoc}
52+
*
53+
* @codeCoverageIgnore
54+
*/
55+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
56+
return new static(
57+
$configuration,
58+
$plugin_id,
59+
$plugin_definition,
60+
$container->get('entity_type.manager')
61+
);
62+
}
63+
64+
/**
65+
* EntityLoad constructor.
66+
*
67+
* @param array $configuration
68+
* The plugin configuration array.
69+
* @param string $plugin_id
70+
* The plugin id.
71+
* @param array $plugin_definition
72+
* The plugin definition array.
73+
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
74+
* The entity type manager service.
75+
*
76+
* @codeCoverageIgnore
77+
*/
78+
public function __construct(
79+
array $configuration,
80+
string $plugin_id,
81+
array $plugin_definition,
82+
EntityTypeManager $entity_type_manager
83+
) {
84+
parent::__construct($configuration, $plugin_id, $plugin_definition);
85+
$this->entityTypeManager = $entity_type_manager;
86+
}
87+
88+
/**
89+
* Resolves the list of fields for a given entity.
90+
*
91+
* Respects the optional context parameters "bundle" and "field_types". If
92+
* bundle context is set it resolves the fields only for that entity bundle.
93+
* The same goes for field types when either base fields of configurable
94+
* fields may be returned.
95+
*
96+
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_definition
97+
* The entity type definition.
98+
* @param array|null $bundle_context
99+
* Bundle context.
100+
* @param array|null $field_types_context
101+
* Field types context.
102+
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $field_context
103+
* Field context.
104+
*/
105+
public function resolve(
106+
EntityTypeInterface $entity_definition,
107+
?array $bundle_context = NULL,
108+
?array $field_types_context = NULL,
109+
FieldContext $field_context
110+
): \Iterator {
111+
$entity_definition->getBundleEntityType();
112+
/** @var \Drupal\Core\Entity\ContentEntityType $value */
113+
if ($entity_definition instanceof ContentEntityType) {
114+
if ($bundle_context) {
115+
$key = $bundle_context['key'];
116+
$id = $entity_definition->id();
117+
$entity_id = $id . '.' . $id . '.' . $key;
118+
$fields = \Drupal::entityManager()->getFieldDefinitions($id, $key);
119+
}
120+
else {
121+
$id = $entity_definition->id();
122+
$entity_id = $id . '.' . $id . '.default';
123+
$fields = \Drupal::entityManager()->getFieldDefinitions($id, $id);
124+
}
125+
126+
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $form_display_context */
127+
$form_display_context = $this->entityTypeManager
128+
->getStorage('entity_form_display')
129+
->load($entity_id);
130+
131+
$field_context->setContextValue('entity_form_display', $form_display_context);
132+
if ($field_types_context) {
133+
$field_types = $field_types_context['key'];
134+
foreach ($fields as $field) {
135+
if ($field_types === 'BASE_FIELDS') {
136+
if ($field instanceof BaseFieldDefinition) {
137+
yield $field;
138+
}
139+
}
140+
elseif ($field_types === 'FIELD_CONFIG') {
141+
if ($field instanceof FieldConfig || $field instanceof BaseFieldOverride) {
142+
yield $field;
143+
}
144+
}
145+
else {
146+
yield $field;
147+
}
148+
}
149+
}
150+
else {
151+
yield from $fields;
152+
}
153+
}
154+
}
155+
156+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition\Fields;
4+
5+
use Drupal\Core\Field\FieldDefinitionInterface;
6+
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
7+
8+
/**
9+
* Retrieves the "default_value" property from a given field definition.
10+
*
11+
* @DataProducer(
12+
* id = "entity_definition_field_default_value",
13+
* name = @Translation("Entity definition field default_value"),
14+
* description = @Translation("Return entity definition field default_value."),
15+
* consumes = {
16+
* "entity_definition_field" = @ContextDefinition("any",
17+
* label = @Translation("Entity definition field")
18+
* )
19+
* },
20+
* produces = @ContextDefinition("string",
21+
* label = @Translation("Entity definition field default_value")
22+
* )
23+
* )
24+
*/
25+
class DefaultValue extends DataProducerPluginBase {
26+
27+
/**
28+
* Resolves the default value property.
29+
*
30+
* @param \Drupal\Core\Field\FieldDefinitionInterface $entity_definition_field
31+
* The entity field definition.
32+
*
33+
* @return string|bool|int|null
34+
* The default value.
35+
*/
36+
public function resolve(FieldDefinitionInterface $entity_definition_field) {
37+
/** @var \Drupal\field\Entity\FieldConfig $entity_definition_field */
38+
$default_value = $entity_definition_field->getDefaultValueLiteral();
39+
if (is_array($default_value)) {
40+
switch ($entity_definition_field->getType()) {
41+
case 'list_integer':
42+
case 'text_long':
43+
return $default_value ? $default_value[0]['value'] : NULL;
44+
45+
case 'boolean':
46+
return (bool) $default_value ? $default_value[0]['value'] : FALSE;
47+
}
48+
}
49+
else {
50+
return $default_value;
51+
}
52+
}
53+
54+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Drupal\graphql\Plugin\GraphQL\DataProducer\EntityDefinition\Fields;
4+
5+
use Drupal\Core\Field\FieldDefinitionInterface;
6+
use Drupal\Core\StringTranslation\TranslatableMarkup;
7+
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
8+
9+
/**
10+
* Retrieves the "description" from a given field definition.
11+
*
12+
* @DataProducer(
13+
* id = "entity_definition_field_description",
14+
* name = @Translation("Entity definition field description"),
15+
* description = @Translation("Return entity definition field description."),
16+
* consumes = {
17+
* "entity_definition_field" = @ContextDefinition("any",
18+
* label = @Translation("Entity definition field")
19+
* )
20+
* },
21+
* produces = @ContextDefinition("string",
22+
* label = @Translation("Entity definition field description")
23+
* )
24+
* )
25+
*/
26+
class Description extends DataProducerPluginBase {
27+
28+
/**
29+
* Resolves the field description.
30+
*
31+
* @param \Drupal\Core\Field\FieldDefinitionInterface $entity_definition_field
32+
* The entity field definition.
33+
*
34+
* @return string|null
35+
* The description.
36+
*/
37+
public function resolve(FieldDefinitionInterface $entity_definition_field): ?string {
38+
$description = $entity_definition_field->getDescription();
39+
// Convert translation object to string.
40+
if ($description instanceof TranslatableMarkup) {
41+
return (string) $description;
42+
}
43+
return $description;
44+
}
45+
46+
}

0 commit comments

Comments
 (0)