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

EntityBundleDeriver::__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
class EntityBundleDeriver extends DeriverBase implements ContainerDeriverInterface {
13
  use StringTranslationTrait;
14
15
  /**
16
   * The entity type manager service.
17
   *
18
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
19
   */
20
  protected $entityTypeManager;
21
22
  /**
23
   * {@inheritdoc}
24
   */
25
  public static function create(ContainerInterface $container, $basePluginId) {
26
    return new static(
27
      $container->get('entity_type.manager')
28
    );
29
  }
30
31
  /**
32
   * {@inheritdoc}
33
   */
34
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
35
    $this->entityTypeManager = $entityTypeManager;
36
  }
37
38
  /**
39
   * {@inheritdoc}
40
   */
41
  public function getDerivativeDefinitions($basePluginDefinition) {
42
    foreach ($this->entityTypeManager->getDefinitions() as $id => $entityType) {
43
      $bundleEntityType = $entityType->getBundleEntityType();
44
45
      $derivative = [
46
        'type' => $bundleEntityType === NULL ? 'String' : StringHelper::camelCase($bundleEntityType),
47
        'description' => $this->t('Retrieves the bundle or bundle name of the entity.'),
48
        'parents' => [StringHelper::camelCase($id)],
49
      ] + $basePluginDefinition;
50
51
      $this->derivatives["entity:$id"] = $derivative;
52
    }
53
54
    return parent::getDerivativeDefinitions($basePluginDefinition);
55
  }
56
57
}
58