View source
<?php
namespace Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql_core\GraphQL\EntityCrudOutputWrapper;
use Drupal\graphql\Plugin\GraphQL\Mutations\MutationPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use GraphQL\Type\Definition\ResolveInfo;
abstract class DeleteEntityBase extends MutationPluginBase implements ContainerFactoryPluginInterface {
use DependencySerializationTrait;
use StringTranslationTrait;
protected $entityTypeManager;
protected $renderer;
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static($configuration, $pluginId, $pluginDefinition, $container
->get('entity_type.manager'), $container
->get('renderer'));
}
public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager, RendererInterface $renderer) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->entityTypeManager = $entityTypeManager;
$this->renderer = $renderer;
}
public function resolve($value, array $args, ResolveContext $context, ResolveInfo $info) {
return $this->renderer
->executeInRenderContext(new RenderContext(), function () use ($value, $args, $context, $info) {
$entityTypeId = $this->pluginDefinition['entity_type'];
$storage = $this->entityTypeManager
->getStorage($entityTypeId);
if (!($entity = $storage
->load($args['id']))) {
return new EntityCrudOutputWrapper(NULL, NULL, [
$this
->t('The requested entity could not be loaded.'),
]);
}
if (!$entity
->access('delete')) {
return new EntityCrudOutputWrapper(NULL, NULL, [
$this
->t('You do not have the necessary permissions to delete this entity.'),
]);
}
try {
$entity
->delete();
} catch (EntityStorageException $exception) {
return new EntityCrudOutputWrapper(NULL, NULL, [
$this
->t('Entity deletion failed with exception: @exception.', [
'@exception' => $exception
->getMessage(),
]),
]);
}
return new EntityCrudOutputWrapper($entity);
});
}
}