1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
6
|
|
|
use Drupal\Core\Entity\ContentEntityInterface; |
7
|
|
|
use Drupal\Core\Entity\EntityInterface; |
8
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
9
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
10
|
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait; |
11
|
|
|
use Drupal\graphql_core\GraphQL\EntityCrudOutputWrapper; |
12
|
|
|
use Drupal\graphql\Plugin\GraphQL\Mutations\MutationPluginBase; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
15
|
|
|
|
16
|
|
|
abstract class CreateEntityBase extends MutationPluginBase implements ContainerFactoryPluginInterface { |
17
|
|
|
use DependencySerializationTrait; |
18
|
|
|
use StringTranslationTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The entity type manager. |
22
|
|
|
* |
23
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
24
|
|
|
*/ |
25
|
|
|
protected $entityTypeManager; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) { |
31
|
|
|
$this->entityTypeManager = $entityTypeManager; |
32
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
39
|
|
|
return new static( |
40
|
|
|
$configuration, |
41
|
|
|
$pluginId, |
42
|
|
|
$pluginDefinition, |
43
|
|
|
$container->get('entity_type.manager') |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function resolve($value, array $args, ResolveInfo $info) { |
51
|
|
|
$entityTypeId = $this->pluginDefinition['entity_type']; |
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
|
|
|
$storage = $this->entityTypeManager->getStorage($entityTypeId); |
68
|
|
|
$entity = $storage->create($input); |
69
|
|
|
return $this->resolveOutput($entity, $args, $info); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Extract entity values from the resolver args. |
74
|
|
|
* |
75
|
|
|
* Loops over all input values and assigns them to their original field names. |
76
|
|
|
* |
77
|
|
|
* @param array $args |
78
|
|
|
* The entity values provided through the resolver args. |
79
|
|
|
* @param \Youshido\GraphQL\Execution\ResolveInfo $info |
80
|
|
|
* the resolve info object. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
* The extracted entity values with their proper, internal field names. |
84
|
|
|
*/ |
85
|
|
|
abstract protected function extractEntityInput(array $args, ResolveInfo $info); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Formats the output of the mutation. |
89
|
|
|
* |
90
|
|
|
* The default implementation wraps the created entity in another object to |
91
|
|
|
* transport possible error messages and constraint violations after applying |
92
|
|
|
* some access checks and input validation. |
93
|
|
|
* |
94
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity |
95
|
|
|
* The created entity. |
96
|
|
|
* @param array $args |
97
|
|
|
* The arguments array. |
98
|
|
|
* @param \Youshido\GraphQL\Execution\ResolveInfo $info |
99
|
|
|
* The resolve info object. |
100
|
|
|
* |
101
|
|
|
* @return mixed |
102
|
|
|
* The output for the created entity. |
103
|
|
|
*/ |
104
|
|
|
protected function resolveOutput(EntityInterface $entity, array $args, ResolveInfo $info) { |
105
|
|
|
if (!$entity->access('create')) { |
106
|
|
|
return new EntityCrudOutputWrapper(NULL, NULL, [ |
107
|
|
|
$this->t('You do not have the necessary permissions to create entities of this type.'), |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($entity instanceof ContentEntityInterface) { |
|
|
|
|
112
|
|
|
if (($violations = $entity->validate()) && $violations->count()) { |
113
|
|
|
return new EntityCrudOutputWrapper(NULL, $violations); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (($status = $entity->save()) && $status === SAVED_NEW) { |
118
|
|
|
return new EntityCrudOutputWrapper($entity); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return NULL; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.