EntityRendered.php in GraphQL 8.3
File
modules/graphql_core/src/Plugin/GraphQL/Fields/Entity/EntityRendered.php
View source
<?php
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\graphql\GraphQL\Cache\CacheableValue;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use GraphQL\Type\Definition\ResolveInfo;
class EntityRendered extends FieldPluginBase implements ContainerFactoryPluginInterface {
use DependencySerializationTrait;
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 resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
if ($value instanceof ContentEntityInterface) {
$mode = isset($args['mode']) ? $args['mode'] : 'full';
$language = $value
->language()
->getId();
$builder = $this->entityTypeManager
->getViewBuilder($value
->getEntityTypeId());
$view = $builder
->view($value, $mode, $language);
$context = new RenderContext();
$result = $this->renderer
->executeInRenderContext($context, function () use ($view) {
return $this->renderer
->render($view);
});
if (!$context
->isEmpty()) {
(yield new CacheableValue((string) $result, [
$context
->pop(),
]));
}
else {
(yield (string) $result);
}
}
}
}
Classes
Name |
Description |
EntityRendered |
Plugin annotation
@GraphQLField(
id = "entity_rendered",
name = "entityRendered",
type = "String",
secure = true,
deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityRenderedDeriver",
) |