-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathCreateEntity.php
115 lines (101 loc) · 3.96 KB
/
CreateEntity.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Drupal\graphql\Plugin\GraphQL\DataProducer\Entity;
use Drupal\Core\Access\AccessResultReasonInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Drupal\graphql\Plugin\GraphQL\DataProducer\EntityValidationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Creates an entity.
*
* @DataProducer(
* id = "create_entity",
* name = @Translation("Create Entity"),
* produces = @ContextDefinition("entity",
* label = @Translation("Entity")
* ),
* consumes = {
* "entity_type" = @ContextDefinition("string",
* label = @Translation("Entity Type"),
* required = TRUE
* ),
* "values" = @ContextDefinition("any",
* label = @Translation("Field values for creating the entity"),
* required = TRUE
* ),
* "entity_return_key" = @ContextDefinition("string",
* label = @Translation("Key name in the returned array where the entity
* will be placed"), required = TRUE
* ),
* "save" = @ContextDefinition("boolean",
* label = @Translation("Save entity"),
* required = FALSE,
* default_value = TRUE,
* ),
* }
* )
*/
class CreateEntity extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
use EntityValidationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static($configuration, $plugin_id, $plugin_definition);
$instance->entityTypeManager = $container->get('entity_type.manager');
return $instance;
}
/**
* Resolve the values for this producer.
*/
public function resolve(string $entity_type, array $values, string $entity_return_key, ?bool $save, $context) {
$storage = $this->entityTypeManager->getStorage($entity_type);
$accessHandler = $this->entityTypeManager->getAccessControlHandler($entity_type);
// Infer the bundle type from the response and return an error if the entity
// type expects one, but one is not present.
$entity_type = $this->entityTypeManager->getDefinition($entity_type);
$bundle = $entity_type->getKey('bundle') && !empty($values[$entity_type->getKey('bundle')]) ? $values[$entity_type->getKey('bundle')] : NULL;
if ($entity_type->getKey('bundle') && !$bundle) {
return [
'errors' => [$this->t('Entity type being created requires a bundle, but none was present.')],
];
};
// Ensure the user has access to create this kind of entity.
$access = $accessHandler->createAccess($bundle, NULL, [], TRUE);
$context->addCacheableDependency($access);
if (!$access->isAllowed()) {
return [
'errors' => [$access instanceof AccessResultReasonInterface && $access->getReason() ? $access->getReason() : $this->t('Access was forbidden.')],
];
}
$entity = $storage->create($values);
// Core does not have a concept of create access for fields, so edit access
// is used instead. This is consistent with how other Drupal APIs handle
// field based create access.
$field_access_errors = [];
foreach ($values as $field_name => $value) {
$create_access = $entity->get($field_name)->access('edit', NULL, TRUE);
if (!$create_access->isALlowed()) {
$field_access_errors[] = sprintf('%s: %s', $field_name, $create_access instanceof AccessResultReasonInterface ? $create_access->getReason() : $this->t('Access was forbidden.'));
}
}
if (!empty($field_access_errors)) {
return ['errors' => $field_access_errors];
}
if ($violation_messages = $this->getViolationMessages($entity)) {
return ['errors' => $violation_messages];
}
if ($save) {
$entity->save();
}
return [
$entity_return_key => $entity,
];
}
}