|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
|
6
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
7
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
8
|
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait; |
|
9
|
|
|
use Drupal\graphql\GraphQL\Type\InputObjectType; |
|
10
|
|
|
use Drupal\graphql_core\GraphQL\EntityCrudOutputWrapper; |
|
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\Mutations\MutationPluginBase; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
13
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
|
14
|
|
|
|
|
15
|
|
|
abstract class CreateEntityBase extends MutationPluginBase implements ContainerFactoryPluginInterface { |
|
16
|
|
|
use DependencySerializationTrait; |
|
17
|
|
|
use StringTranslationTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The entity type manager. |
|
21
|
|
|
* |
|
22
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $entityTypeManager; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) { |
|
30
|
|
|
$this->entityTypeManager = $entityTypeManager; |
|
31
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
|
38
|
|
|
return new static( |
|
39
|
|
|
$configuration, |
|
40
|
|
|
$pluginId, |
|
41
|
|
|
$pluginDefinition, |
|
42
|
|
|
$container->get('entity_type.manager') |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public function resolve($value, array $args, ResolveInfo $info) { |
|
50
|
|
|
$entityTypeId = $this->pluginDefinition['entity_type']; |
|
51
|
|
|
$storage = $this->entityTypeManager->getStorage($entityTypeId); |
|
52
|
|
|
|
|
53
|
|
|
// The raw input needs to be converted to use the proper field and property |
|
54
|
|
|
// keys because we usually convert them to camel case when adding them to |
|
55
|
|
|
// the schema. |
|
56
|
|
|
$input = $this->extractEntityInput($args, $info); |
|
57
|
|
|
|
|
58
|
|
|
$entityDefinition = $this->entityTypeManager->getDefinition($entityTypeId); |
|
59
|
|
|
if ($entityDefinition->hasKey('bundle')) { |
|
60
|
|
|
$bundleName = $this->pluginDefinition['entity_bundle']; |
|
61
|
|
|
$bundleKey = $entityDefinition->getKey('bundle'); |
|
62
|
|
|
|
|
63
|
|
|
// Add the entity's bundle with the correct key. |
|
64
|
|
|
$input[$bundleKey] = $bundleName; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ |
|
68
|
|
|
$entity = $storage->create($input); |
|
69
|
|
|
|
|
70
|
|
|
if (!$entity->access('create')) { |
|
71
|
|
|
return new EntityCrudOutputWrapper(NULL, NULL, [ |
|
72
|
|
|
$this->t('You do not have the necessary permissions to create entities of this type.'), |
|
73
|
|
|
]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if (($violations = $entity->validate()) && $violations->count()) { |
|
77
|
|
|
return new EntityCrudOutputWrapper(NULL, $violations); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (($status = $entity->save()) && $status === SAVED_NEW) { |
|
81
|
|
|
return new EntityCrudOutputWrapper($entity); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return NULL; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Extract entity values from the resolver args. |
|
89
|
|
|
* |
|
90
|
|
|
* Loops over all input values and assigns them to their original field names. |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $args |
|
93
|
|
|
* The entity values provided through the resolver args. |
|
94
|
|
|
* @param \Youshido\GraphQL\Execution\ResolveInfo $info |
|
95
|
|
|
* the resolve info object. |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
* The extracted entity values with their proper, internal field names. |
|
99
|
|
|
*/ |
|
100
|
|
|
abstract protected function extractEntityInput(array $args, ResolveInfo $info); |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|