EntityRenderedDeriver.php in GraphQL 8.3
File
modules/graphql_core/src/Plugin/Deriver/Fields/EntityRenderedDeriver.php
View source
<?php
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\graphql\Utility\StringHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityRenderedDeriver extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $entityDisplayRepository;
public static function create(ContainerInterface $container, $basePluginId) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_display.repository'));
}
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityDisplayRepositoryInterface $entityDisplayRepository) {
$this->entityTypeManager = $entityTypeManager;
$this->entityDisplayRepository = $entityDisplayRepository;
}
public function getDerivativeDefinitions($basePluginDefinition) {
foreach ($this->entityTypeManager
->getDefinitions() as $id => $type) {
if ($type instanceof ContentEntityTypeInterface && !empty($this->entityDisplayRepository
->getViewModes($id))) {
$derivative = [
'parents' => [
StringHelper::camelCase($id),
],
'description' => $this
->t("Renders '@type' entities in the given view mode.", [
'@type' => $type
->getLabel(),
]),
'entity_type' => $id,
] + $basePluginDefinition;
if (!isset($derivative['arguments']['mode'])) {
$derivative['arguments'] = isset($derivative['arguments']) ? $derivative['arguments'] : [];
$derivative['arguments']['mode'] = [
'type' => StringHelper::camelCase($id, 'display', 'mode', 'id'),
'optional' => TRUE,
];
}
$this->derivatives["entity:{$id}"] = $derivative;
}
}
return parent::getDerivativeDefinitions($basePluginDefinition);
}
}