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

EntityFieldDeriverBase::isSinglePropertyField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 3
nop 1
rs 10
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
   * @return array
32
   *   The derived plugin definitions for the given field.
33
   */
34
  abstract protected function getDerivativeDefinitionsFromFieldDefinition($entityTypeId, FieldStorageDefinitionInterface $fieldDefinition, array $basePluginDefinition);
35
36
  /**
37
   * The entity type manager.
38
   *
39
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
40
   */
41
  protected $entityTypeManager;
42
43
  /**
44
   * The entity field manager.
45
   *
46
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
47
   */
48
  protected $entityFieldManager;
49
50
  /**
51
   * The entity bundle info.
52
   *
53
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
54
   */
55
  protected $entityBundleInfo;
56
57
  /**
58
   * The base plugin id.
59
   *
60
   * @var string
61
   */
62
  protected $basePluginId;
63
64
  /**
65
   * {@inheritdoc}
66
   */
67
  public static function create(ContainerInterface $container, $basePluginId) {
68
    return new static(
69
      $container->get('entity_type.manager'),
70
      $container->get('entity_field.manager'),
71
      $container->get('entity_type.bundle.info'),
72
      $basePluginId
73
    );
74
  }
75
76
  /**
77
   * RawValueFieldItemDeriver constructor.
78
   *
79
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
80
   *   The entity type manager.
81
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
82
   *   The entity field manager.
83
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
84
   *   The bundle info service.
85
   * @param string $basePluginId
86
   *   The base plugin id.
87
   */
88
  public function __construct(
89
    EntityTypeManagerInterface $entityTypeManager,
90
    EntityFieldManagerInterface $entityFieldManager,
91
    EntityTypeBundleInfoInterface $entityTypeBundleInfo,
92
    $basePluginId
93
  ) {
94
    $this->basePluginId = $basePluginId;
95
    $this->entityTypeManager = $entityTypeManager;
96
    $this->entityFieldManager = $entityFieldManager;
97
    $this->entityBundleInfo = $entityTypeBundleInfo;
98
  }
99
100
  /**
101
   * {@inheritdoc}
102
   */
103
  public function getDerivativeDefinitions($basePluginDefinition) {
104
    foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
105
      if (!$entityType->entityClassImplements(FieldableEntityInterface::class)) {
106
        continue;
107
      }
108
109
      foreach ($this->entityFieldManager->getFieldStorageDefinitions($entityTypeId) as $fieldStorageDefinition) {
110
        if ($derivatives = $this->getDerivativeDefinitionsFromFieldDefinition($entityTypeId, $fieldStorageDefinition, $basePluginDefinition)) {
111
          $this->derivatives = array_merge($this->derivatives, $derivatives);
112
        }
113
      }
114
    }
115
116
    return $this->derivatives;
117
  }
118
119
}
120