Completed
Push — 8.x-3.x ( 3134b4...817b25 )
by Sebastian
02:47
created

EntityFieldPropertyDeriver::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
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
    $parents = [];
64
    foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
65
      $interfaces = class_implements($entityType->getClass());
66
      if (!array_key_exists(FieldableEntityInterface::class, $interfaces)) {
67
        continue;
68
      }
69
70
      $fieldDefinitions = $this->entityFieldManager->getFieldStorageDefinitions($entityTypeId);
71
      foreach ($fieldDefinitions as $fieldDefinition) {
72
        $fieldName = $fieldDefinition->getName();
73
        $fieldType = $fieldDefinition->getType();
74
75
        if (isset($basePluginDefinition['field_types']) && in_array($fieldType, $basePluginDefinition['field_types'])) {
76
          $parents[] = EntityFieldType::getId($entityTypeId, $fieldName);
77
        }
78
      }
79
    }
80
81
    if (!empty($parents)) {
82
      $this->derivatives[$basePluginDefinition['id']] = [
83
        'parents' => array_merge($parents, $basePluginDefinition['parents']),
84
      ] + $basePluginDefinition;
85
    }
86
87
    return $this->derivatives;
88
  }
89
}