You are here

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;

/**
 * @GraphQLField(
 *   id = "entity_translations",
 *   name = "entityTranslations",
 *   secure = true,
 *   type = "[Entity]",
 *   parents = {"Entity"}
 * )
 */
class EntityTranslations extends FieldPluginBase implements ContainerFactoryPluginInterface {
  use DependencySerializationTrait;

  /**
   * The entity repository service.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
    return new static($configuration, $pluginId, $pluginDefinition, $container
      ->get('entity.repository'));
  }

  /**
   * EntityTranslations constructor.
   *
   * @param array $configuration
   *   The plugin configuration array.
   * @param string $pluginId
   *   The plugin id.
   * @param mixed $pluginDefinition
   *   The plugin definition array.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
   *   The entity repository service.
   */
  public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityRepositoryInterface $entityRepository) {
    parent::__construct($configuration, $pluginId, $pluginDefinition);
    $this->entityRepository = $entityRepository;
  }

  /**
   * {@inheritdoc}
   */
  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

Namesort descending Description
EntityTranslations Plugin annotation @GraphQLField( id = "entity_translations", name = "entityTranslations", secure = true, type = "[Entity]", parents = {"Entity"} )