EntityTranslations.php in GraphQL 8.3
File
modules/graphql_core/src/Plugin/GraphQL/Fields/Entity/EntityTranslations.php
View source
<?php
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use GraphQL\Type\Definition\ResolveInfo;
class EntityTranslations extends FieldPluginBase implements ContainerFactoryPluginInterface {
use DependencySerializationTrait;
protected $entityRepository;
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static($configuration, $pluginId, $pluginDefinition, $container
->get('entity.repository'));
}
public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityRepositoryInterface $entityRepository) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->entityRepository = $entityRepository;
}
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
if ($value instanceof EntityInterface && $value instanceof TranslatableInterface && $value
->isTranslatable()) {
$languages = $value
->getTranslationLanguages();
foreach ($languages as $language) {
if ($value
->hasTranslation($language
->getId())) {
(yield $value
->getTranslation($language
->getId()));
}
}
}
}
}
Classes
Name |
Description |
EntityTranslations |
Plugin annotation
@GraphQLField(
id = "entity_translations",
name = "entityTranslations",
secure = true,
type = "[Entity]",
parents = {"Entity"}
) |