Completed
Pull Request — 8.x-3.x (#400)
by Sebastian
02:15
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\Entity\EntityTypeBundleInfoInterface;
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\Core\Entity\EntityFieldManagerInterface;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * Generate GraphQLField plugins for config fields.
16
 */
17
abstract class EntityFieldDeriverBase extends DeriverBase implements ContainerDeriverInterface {
18
19
  /**
20
   * Provides plugin definition values from fields.
21
   *
22
   * @param string $entityTypeId
23
   *   The host entity type.
24
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $fieldDefinition
25
   *   Field definition object.
26
   * @param array $basePluginDefinition
27
   *   Base definition array.
28
   */
29
  abstract protected function getDerivativeDefinitionsFromFieldDefinition($entityTypeId, FieldStorageDefinitionInterface $fieldDefinition, array $basePluginDefinition);
30
31
  /**
32
   * The entity type manager.
33
   *
34
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
35
   */
36
  protected $entityTypeManager;
37
38
  /**
39
   * The entity field manager.
40
   *
41
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
42
   */
43
  protected $entityFieldManager;
44
45
  /**
46
   * The entity bundle info.
47
   *
48
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
49
   */
50
  protected $entityBundleInfo;
51
52
  /**
53
   * The base plugin id.
54
   *
55
   * @var string
56
   */
57
  protected $basePluginId;
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public static function create(ContainerInterface $container, $basePluginId) {
63
    return new static(
64
      $container->get('entity_type.manager'),
65
      $container->get('entity_field.manager'),
66
      $container->get('entity_type.bundle.info'),
67
      $basePluginId
68
    );
69
  }
70
71
  /**
72
   * RawValueFieldItemDeriver constructor.
73
   *
74
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
75
   *   The entity type manager.
76
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
77
   *   The entity field manager.
78
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
79
   *   The bundle info service.
80
   * @param string $basePluginId
81
   *   The base plugin id.
82
   */
83
  public function __construct(
84
    EntityTypeManagerInterface $entityTypeManager,
85
    EntityFieldManagerInterface $entityFieldManager,
86
    EntityTypeBundleInfoInterface $entityTypeBundleInfo,
87
    $basePluginId
88
  ) {
89
    $this->basePluginId = $basePluginId;
90
    $this->entityTypeManager = $entityTypeManager;
91
    $this->entityFieldManager = $entityFieldManager;
92
    $this->entityBundleInfo = $entityTypeBundleInfo;
93
  }
94
95
  /**
96
   * {@inheritdoc}
97
   */
98
  public function getDerivativeDefinitions($basePluginDefinition) {
99
    $this->derivatives = [];
100
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 ($fieldStorageDefinition->isBaseField()) {
109
          $this->derivatives = array_merge($this->derivatives, $this->getDerivativeDefinitionsFromFieldDefinition($entityTypeId, $fieldStorageDefinition, $basePluginDefinition));
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