You are here

EntityRevisionByIdDeriver.php in GraphQL 8.3

File

modules/graphql_core/src/Plugin/Deriver/Fields/EntityRevisionByIdDeriver.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 EntityRevisionByIdDeriver extends DeriverBase implements ContainerDeriverInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $basePluginId) {
    return new static($container
      ->get('entity_type.manager'));
  }

  /**
   * EntityRevisionByIdDeriver constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager service.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($basePluginDefinition) {
    foreach ($this->entityTypeManager
      ->getDefinitions() as $id => $type) {
      if ($type instanceof ContentEntityTypeInterface && $type
        ->isRevisionable()) {
        $derivative = [
          'name' => StringHelper::propCase($id, 'revision', 'by', 'id'),
          'type' => "entity:{$id}",
          'description' => $this
            ->t("Loads '@type' entity revision by their revision 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);
  }

}

Classes