|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
|
|
|
|
|
|
6
|
|
|
use Drupal\Core\Entity\EntityStorageException; |
|
|
|
|
|
|
7
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
|
|
|
|
|
8
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
|
|
|
|
|
9
|
|
|
use Drupal\Core\Render\RenderContext; |
|
|
|
|
|
|
10
|
|
|
use Drupal\Core\Render\RendererInterface; |
|
|
|
|
|
|
11
|
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait; |
|
|
|
|
|
|
12
|
|
|
use Drupal\graphql\GraphQL\Execution\ResolveContext; |
|
13
|
|
|
use Drupal\graphql_core\GraphQL\EntityCrudOutputWrapper; |
|
14
|
|
|
use Drupal\graphql\Plugin\GraphQL\Mutations\MutationPluginBase; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
|
|
|
|
16
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
17
|
|
|
|
|
18
|
|
|
abstract class DeleteEntityBase extends MutationPluginBase implements ContainerFactoryPluginInterface { |
|
19
|
|
|
use DependencySerializationTrait; |
|
20
|
|
|
use StringTranslationTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The entity type manager. |
|
24
|
|
|
* |
|
25
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $entityTypeManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The renderer service. |
|
31
|
|
|
* |
|
32
|
|
|
* @var \Drupal\Core\Render\RendererInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $renderer; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
|
40
|
|
|
return new static( |
|
41
|
|
|
$configuration, |
|
42
|
|
|
$pluginId, |
|
43
|
|
|
$pluginDefinition, |
|
44
|
|
|
$container->get('entity_type.manager'), |
|
45
|
|
|
$container->get('renderer') |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* DeleteEntityBase constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $configuration |
|
53
|
|
|
* The plugin configuration array. |
|
54
|
|
|
* @param string $pluginId |
|
55
|
|
|
* The plugin id. |
|
56
|
|
|
* @param mixed $pluginDefinition |
|
57
|
|
|
* The plugin definition array. |
|
58
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
|
59
|
|
|
* The entity type manager service. |
|
60
|
|
|
* @param \Drupal\Core\Render\RendererInterface $renderer |
|
61
|
|
|
* The renderer service. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager, RendererInterface $renderer) { |
|
64
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
65
|
|
|
$this->entityTypeManager = $entityTypeManager; |
|
66
|
|
|
$this->renderer = $renderer; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function resolve($value, array $args, ResolveContext $context, ResolveInfo $info) { |
|
73
|
|
|
// There are cases where the Drupal entity API calls emit the cache metadata |
|
74
|
|
|
// in the current render context. In such cases |
|
75
|
|
|
// EarlyRenderingControllerWrapperSubscriber throws the leaked cache |
|
76
|
|
|
// metadata exception. To avoid this, wrap the execution in its own render |
|
77
|
|
|
// context. |
|
78
|
|
|
return $this->renderer->executeInRenderContext(new RenderContext(), function () use ($value, $args, $context, $info) { |
|
|
|
|
|
|
79
|
|
|
$entityTypeId = $this->pluginDefinition['entity_type']; |
|
80
|
|
|
$storage = $this->entityTypeManager->getStorage($entityTypeId); |
|
81
|
|
|
|
|
82
|
|
|
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ |
|
83
|
|
|
if (!$entity = $storage->load($args['id'])) { |
|
84
|
|
|
return new EntityCrudOutputWrapper(NULL, NULL, [ |
|
85
|
|
|
$this->t('The requested entity could not be loaded.'), |
|
86
|
|
|
]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (!$entity->access('delete')) { |
|
90
|
|
|
return new EntityCrudOutputWrapper(NULL, NULL, [ |
|
91
|
|
|
$this->t('You do not have the necessary permissions to delete this entity.'), |
|
92
|
|
|
]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
try { |
|
96
|
|
|
$entity->delete(); |
|
97
|
|
|
} |
|
98
|
|
|
catch (EntityStorageException $exception) { |
|
99
|
|
|
return new EntityCrudOutputWrapper(NULL, NULL, [ |
|
100
|
|
|
$this->t('Entity deletion failed with exception: @exception.', [ |
|
101
|
|
|
'@exception' => $exception->getMessage(), |
|
102
|
|
|
]), |
|
103
|
|
|
]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return new EntityCrudOutputWrapper($entity); |
|
107
|
|
|
}); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths