EntityByIdDeriver.php in GraphQL 8.3
File
modules/graphql_core/src/Plugin/Deriver/Fields/EntityByIdDeriver.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\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\graphql\Utility\StringHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityByIdDeriver extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected $entityTypeManager;
public static function create(ContainerInterface $container, $basePluginId) {
return new static($container
->get('entity_type.manager'));
}
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public function getDerivativeDefinitions($basePluginDefinition) {
foreach ($this->entityTypeManager
->getDefinitions() as $id => $type) {
if ($type instanceof ContentEntityTypeInterface) {
$derivative = [
'name' => StringHelper::propCase($id, 'by', 'id'),
'type' => "entity:{$id}",
'description' => $this
->t("Loads '@type' entities by their id.", [
'@type' => $type
->getLabel(),
]),
'entity_type' => $id,
] + $basePluginDefinition;
if ($type
->isTranslatable()) {
$derivative['arguments']['language'] = [
'type' => 'LanguageId',
];
}
$this->derivatives["entity:{$id}"] = $derivative;
}
}
return parent::getDerivativeDefinitions($basePluginDefinition);
}
}