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

EntityFieldDeriverBase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isSinglePropertyField() 0 4 1
getDerivativeDefinitionsFromFieldDefinition() 0 1 ?
A create() 0 8 1
A __construct() 0 11 1
B getDerivativeDefinitions() 0 16 5
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver;
4
5
use Drupal\Component\Plugin\Derivative\DeriverBase;
6
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
7
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8
use Drupal\Core\Entity\EntityTypeManagerInterface;
9
use Drupal\Core\Entity\FieldableEntityInterface;
10
use Drupal\Core\Field\FieldStorageDefinitionInterface;
11
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
12
use Drupal\Core\Entity\EntityFieldManagerInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
/**
16
 * Generate GraphQLField plugins for config fields.
17
 */
18
abstract class EntityFieldDeriverBase extends DeriverBase implements ContainerDeriverInterface {
19
  use DependencySerializationTrait;
20
21
  /**
22
   * Provides plugin definition values from fields.
23
   *
24
   * @param string $entityTypeId
25
   *   The host entity type.
26
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $fieldDefinition
27
   *   Field definition object.
28
   * @param array $basePluginDefinition
29
   *   Base definition array.
30
   */
31
  abstract protected function getDerivativeDefinitionsFromFieldDefinition($entityTypeId, FieldStorageDefinitionInterface $fieldDefinition, array $basePluginDefinition);
32
33
  /**
34
   * The entity type manager.
35
   *
36
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
37
   */
38
  protected $entityTypeManager;
39
40
  /**
41
   * The entity field manager.
42
   *
43
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
44
   */
45
  protected $entityFieldManager;
46
47
  /**
48
   * The entity bundle info.
49
   *
50
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
51
   */
52
  protected $entityBundleInfo;
53
54
  /**
55
   * The base plugin id.
56
   *
57
   * @var string
58
   */
59
  protected $basePluginId;
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public static function create(ContainerInterface $container, $basePluginId) {
65
    return new static(
66
      $container->get('entity_type.manager'),
67
      $container->get('entity_field.manager'),
68
      $container->get('entity_type.bundle.info'),
69
      $basePluginId
70
    );
71
  }
72
73
  /**
74
   * RawValueFieldItemDeriver constructor.
75
   *
76
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
77
   *   The entity type manager.
78
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
79
   *   The entity field manager.
80
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
81
   *   The bundle info service.
82
   * @param string $basePluginId
83
   *   The base plugin id.
84
   */
85
  public function __construct(
86
    EntityTypeManagerInterface $entityTypeManager,
87
    EntityFieldManagerInterface $entityFieldManager,
88
    EntityTypeBundleInfoInterface $entityTypeBundleInfo,
89
    $basePluginId
90
  ) {
91
    $this->basePluginId = $basePluginId;
92
    $this->entityTypeManager = $entityTypeManager;
93
    $this->entityFieldManager = $entityFieldManager;
94
    $this->entityBundleInfo = $entityTypeBundleInfo;
95
  }
96
97
  /**
98
   * {@inheritdoc}
99
   */
100
  public function getDerivativeDefinitions($basePluginDefinition) {
101
    foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
102
      $interfaces = class_implements($entityType->getClass());
103
      if (!array_key_exists(FieldableEntityInterface::class, $interfaces)) {
104
        continue;
105
      }
106
107
      foreach ($this->entityFieldManager->getFieldStorageDefinitions($entityTypeId) as $fieldStorageDefinition) {
108
        if ($derivatives = $this->getDerivativeDefinitionsFromFieldDefinition($entityTypeId, $fieldStorageDefinition, $basePluginDefinition)) {
109
          $this->derivatives = array_merge($this->derivatives, $derivatives);
110
        }
111
      }
112
    }
113
114
    return $this->derivatives;
115
  }
116
117
  /**
118
   * Tells if given field has just a single property.
119
   *
120
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
121
   *   Field definition.
122
   *
123
   * @return bool
124
   */
125
  protected function isSinglePropertyField(FieldStorageDefinitionInterface $definition) {
126
    $properties = $definition->getPropertyDefinitions();
127
    return count($properties) === 1;
128
  }
129
130
}
131