1 | <?php |
||
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) { |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
||
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 |