You are here

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;

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

  /**
   * The entity display repository service.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
   */
  protected $entityDisplayRepository;

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

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

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

}

Classes