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

getDerivativeDefinitionsFromFieldDefinition()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 3
dl 0
loc 26
c 0
b 0
f 0
cc 3
eloc 19
nop 4
rs 8.8571
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
4
5
use Drupal\Core\Field\FieldStorageDefinitionInterface;
6
use Drupal\graphql\Utility\StringHelper;
7
use Drupal\graphql_core\Plugin\Deriver\EntityFieldDeriverWithTypeMapping;
8
use Drupal\graphql_core\Plugin\GraphQL\Types\Entity\EntityFieldType;
9
10
class EntityFieldItemDeriver extends EntityFieldDeriverWithTypeMapping {
11
12
  /**
13
   * {@inheritdoc}
14
   */
15
  protected function getDerivativeDefinitionsFromFieldDefinition($entityTypeId, FieldStorageDefinitionInterface $definition, array $basePluginDefinition, $bundleId = NULL) {
16
    $fieldName = $definition->getName();
17
    $dataType = EntityFieldType::getId($entityTypeId, $fieldName);
18
19
    $derivatives = [];
20
    $propertyDefinitions = $definition->getPropertyDefinitions();
21
    foreach ($propertyDefinitions as $property => $propertyDefinition) {
22
      if ($propertyDefinition->getDataType() == 'map') {
23
        // TODO Is it possible to get the keys of a map (eg. the options array for link field) here?
24
        continue;
25
      }
26
27
      $derivatives["$entityTypeId-$fieldName-$property"] = [
28
        'name' => StringHelper::propCase($property),
29
        'property' => $property,
30
        'multi' => FALSE,
31
        'type' => $this->typeMapper->typedDataToGraphQLFieldType($propertyDefinition),
32
        'parents' => [$dataType],
33
        'schema_cache_tags' => array_merge($definition->getCacheTags(), ['entity_field_info']),
34
        'schema_cache_contexts' => $definition->getCacheContexts(),
35
        'schema_cache_max_age' => $definition->getCacheMaxAge(),
36
      ] + $basePluginDefinition;
37
    }
38
39
    return $derivatives;
40
  }
41
42
}
43