Completed
Pull Request — 8.x-3.x (#477)
by Sebastian
02:50
created

EntityByIdDeriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
4
5
use Drupal\Component\Plugin\Derivative\DeriverBase;
6
use Drupal\Core\Entity\EntityTypeManagerInterface;
7
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
8
use Drupal\Core\StringTranslation\StringTranslationTrait;
9
use Drupal\graphql\Utility\StringHelper;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
/**
13
 * Create GraphQL entityById fields based on available Drupal entity types.
14
 */
15
class EntityByIdDeriver extends DeriverBase implements ContainerDeriverInterface {
16
17
  use StringTranslationTrait;
18
19
  /**
20
   * The entity type manager service.
21
   *
22
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
23
   */
24
  protected $entityTypeManager;
25
26
  /**
27
   * {@inheritdoc}
28
   */
29
  public static function create(ContainerInterface $container, $basePluginId) {
30
    return new static($container->get('entity_type.manager'));
31
  }
32
33
  /**
34
   * {@inheritdoc}
35
   */
36
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
37
    $this->entityTypeManager = $entityTypeManager;
38
  }
39
40
  /**
41
   * {@inheritdoc}
42
   */
43
  public function getDerivativeDefinitions($basePluginDefinition) {
44
    foreach ($this->entityTypeManager->getDefinitions() as $id => $type) {
45
      $derivative = [
46
        'name' => StringHelper::propCase($id, 'by', 'id'),
47
        'type' => "entity:$id",
48
        'description' => $this->t("Loads '@type' entities by their id.", ['@type' => $type->getLabel()]),
49
        'entity_type' => $id,
50
      ] + $basePluginDefinition;
51
52
      if ($type->isTranslatable()) {
53
        $derivative['arguments']['language'] = [
54
          'type' => 'AvailableLanguages',
55
        ];
56
      }
57
58
      $this->derivatives["entity:$id"] = $derivative;
59
    }
60
61
    return parent::getDerivativeDefinitions($basePluginDefinition);
62
  }
63
64
}
65