View source
<?php
namespace Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
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 UpdateEntityBase 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'];
$bundleName = $this->pluginDefinition['entity_bundle'];
$storage = $this->entityTypeManager
->getStorage($entityTypeId);
if (!($entity = $storage
->load($args['id']))) {
return new EntityCrudOutputWrapper(NULL, NULL, [
$this
->t('The requested @bundle could not be loaded.', [
'@bundle' => $bundleName,
]),
]);
}
if (!$entity
->bundle() === $bundleName) {
return new EntityCrudOutputWrapper(NULL, NULL, [
$this
->t('The requested entity is not of the expected type @bundle.', [
'@bundle' => $bundleName,
]),
]);
}
if (!$entity
->access('update')) {
return new EntityCrudOutputWrapper(NULL, NULL, [
$this
->t('You do not have the necessary permissions to update this @bundle.', [
'@bundle' => $bundleName,
]),
]);
}
$input = $this
->extractEntityInput($value, $args, $context, $info);
try {
foreach ($input as $key => $value) {
$entity
->get($key)
->setValue($value);
}
} catch (\InvalidArgumentException $exception) {
return new EntityCrudOutputWrapper(NULL, NULL, [
$this
->t('The entity update failed with exception: @exception.', [
'@exception' => $exception
->getMessage(),
]),
]);
}
if (($violations = $entity
->validate()) && $violations
->count()) {
return new EntityCrudOutputWrapper(NULL, $violations);
}
if (($status = $entity
->save()) && $status === SAVED_UPDATED) {
return new EntityCrudOutputWrapper($entity);
}
return NULL;
});
}
protected abstract function extractEntityInput($value, array $args, ResolveContext $context, ResolveInfo $info);
}