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

EntityBundleDeriver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 5 1
A __construct() 0 3 1
A getDerivativeDefinitions() 0 15 3
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