Completed
Push — 8.x-3.x ( bd3169...9e14ee )
by Philipp
02:08
created

EntityFieldPropertyDeriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 9.4285
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
4
5
use Drupal\Component\Plugin\Derivative\DeriverBase;
6
use Drupal\Core\Entity\EntityFieldManagerInterface;
7
use Drupal\Core\Entity\EntityTypeManagerInterface;
8
use Drupal\Core\Entity\FieldableEntityInterface;
9
use Drupal\Core\Field\FieldStorageDefinitionInterface;
10
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
11
use Drupal\graphql_core\Plugin\GraphQL\Types\Entity\EntityFieldType;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * Attach new properties to field types.
16
 */
17
class EntityFieldPropertyDeriver extends DeriverBase implements ContainerDeriverInterface {
18
19
  /**
20
   * The entity type manager.
21
   *
22
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
23
   */
24
  protected $entityTypeManager;
25
26
  /**
27
   * The entity field manager.
28
   *
29
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
30
   */
31
  protected $entityFieldManager;
32
33
  /**
34
   * {@inheritdoc}
35
   */
36
  public static function create(ContainerInterface $container, $basePluginId) {
37
    return new static(
38
      $container->get('entity_type.manager'),
39
      $container->get('entity_field.manager')
40
    );
41
  }
42
43
  /**
44
   * RawValueFieldItemDeriver constructor.
45
   *
46
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
47
   *   The entity type manager.
48
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
49
   *   The entity field manager.
50
   */
51
  public function __construct(
52
    EntityTypeManagerInterface $entityTypeManager,
53
    EntityFieldManagerInterface $entityFieldManager
54
  ) {
55
    $this->entityTypeManager = $entityTypeManager;
56
    $this->entityFieldManager = $entityFieldManager;
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public function getDerivativeDefinitions($basePluginDefinition) {
63
    $this->derivatives = [];
64
65
    $parents = [];
66
67
    foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
68
      $interfaces = class_implements($entityType->getClass());
69
      if (!array_key_exists(FieldableEntityInterface::class, $interfaces)) {
70
        continue;
71
      }
72
73
      $fieldDefinitions = $this->entityFieldManager
74
        ->getFieldStorageDefinitions($entityTypeId);
75
76
      foreach ($fieldDefinitions as $fieldDefinition) {
77
        $fieldName = $fieldDefinition->getName();
78
        $fieldType = $fieldDefinition->getType();
79
80
        if (isset($basePluginDefinition['field_types']) && in_array($fieldType, $basePluginDefinition['field_types'])) {
81
          $parents[] = EntityFieldType::getId($entityTypeId, $fieldName);
82
        }
83
      }
84
    }
85
86
    $this->derivatives[$basePluginDefinition['id']] = [
87
      'parents' => $parents,
88
    ] + $basePluginDefinition;
89
90
    return $this->derivatives;
91
  }
92
}