Completed
Pull Request — 8.x-3.x (#571)
by Philipp
02:54
created

getAccessiblePropertyDefinitions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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 Drupal\Core\TypedData\DataDefinitionInterface;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
16
/**
17
 * Generate GraphQLField plugins for config fields.
18
 */
19
abstract class EntityFieldDeriverBase extends DeriverBase implements ContainerDeriverInterface {
20
  use DependencySerializationTrait;
21
22
  /**
23
   * Provides plugin definition values from fields.
24
   *
25
   * @param string $entityTypeId
26
   *   The host entity type.
27
   * @param string $bundle
28
   *   The host bundle.
29
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $fieldDefinition
30
   *   Field definition object.
31
   * @param array $basePluginDefinition
32
   *   Base definition array.
33
   *
34
   * @return array
35
   *   The derived plugin definitions for the given field.
36
   */
37
  abstract protected function getDerivativeDefinitionsFromFieldDefinition($entityTypeId, $bundle, FieldStorageDefinitionInterface $fieldDefinition, array $basePluginDefinition);
38
39
  /**
40
   * The entity type manager.
41
   *
42
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
43
   */
44
  protected $entityTypeManager;
45
46
  /**
47
   * The entity field manager.
48
   *
49
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
50
   */
51
  protected $entityFieldManager;
52
53
  /**
54
   * The entity bundle info.
55
   *
56
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
57
   */
58
  protected $entityBundleInfo;
59
60
  /**
61
   * The base plugin id.
62
   *
63
   * @var string
64
   */
65
  protected $basePluginId;
66
67
  /**
68
   * {@inheritdoc}
69
   */
70
  public static function create(ContainerInterface $container, $basePluginId) {
71
    return new static(
72
      $container->get('entity_type.manager'),
73
      $container->get('entity_field.manager'),
74
      $container->get('entity_type.bundle.info'),
75
      $basePluginId
76
    );
77
  }
78
79
  /**
80
   * RawValueFieldItemDeriver constructor.
81
   *
82
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
83
   *   The entity type manager.
84
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
85
   *   The entity field manager.
86
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
87
   *   The bundle info service.
88
   * @param string $basePluginId
89
   *   The base plugin id.
90
   */
91
  public function __construct(
92
    EntityTypeManagerInterface $entityTypeManager,
93
    EntityFieldManagerInterface $entityFieldManager,
94
    EntityTypeBundleInfoInterface $entityTypeBundleInfo,
95
    $basePluginId
96
  ) {
97
    $this->basePluginId = $basePluginId;
98
    $this->entityTypeManager = $entityTypeManager;
99
    $this->entityFieldManager = $entityFieldManager;
100
    $this->entityBundleInfo = $entityTypeBundleInfo;
101
  }
102
103
  /**
104
   * {@inheritdoc}
105
   */
106
  public function getDerivativeDefinitions($basePluginDefinition) {
107
    foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
108
      if (!$entityType->entityClassImplements(FieldableEntityInterface::class)) {
109
        continue;
110
      }
111
112
      foreach ($this->entityFieldManager->getFieldStorageDefinitions($entityTypeId) as $fieldStorageDefinition) {
113
        if ($derivatives = $this->getDerivativeDefinitionsFromFieldDefinition($entityTypeId, $fieldStorageDefinition, $basePluginDefinition)) {
114
          $this->derivatives = array_merge($this->derivatives, $derivatives);
115
        }
116
      }
117
    }
118
119
    return $this->derivatives;
120
  }
121
122
  /**
123
   * Get accessible (non-internal) properties of a field.
124
   *
125
   * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $fieldStorageDefinition
126
   *   The field storage definition.
127
   *
128
   * @return DataDefinitionInterface[]
129
   *   The property data definitions.
130
   */
131
  protected function getAccessiblePropertyDefinitions(FieldStorageDefinitionInterface $fieldStorageDefinition) {
132
    return array_filter($fieldStorageDefinition->getPropertyDefinitions(), function (DataDefinitionInterface $definition) {
133
      return !method_exists($definition, 'isInternal') || !$definition->isInternal();
134
    });
135
  }
136
137
}
138