Completed
Push — 8.x-3.x ( 507848...32ff4d )
by Sebastian
02:30
created

getDerivativeDefinitionsFromFieldDefinition()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 3
nop 4
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
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 $fieldDefinition, array $basePluginDefinition, $bundleId = NULL) {
16
    $derivatives = [];
17
    $fieldName = $fieldDefinition->getName();
18
    $commonDefinition = [
19
      'parents' => [EntityFieldType::getId($entityTypeId, $fieldName)],
20
      'schema_cache_tags' => array_merge($fieldDefinition->getCacheTags(), ['entity_field_info']),
21
      'schema_cache_contexts' => $fieldDefinition->getCacheContexts(),
22
      'schema_cache_max_age' => $fieldDefinition->getCacheMaxAge(),
23
    ];
24
25
    foreach ($fieldDefinition->getPropertyDefinitions() as $property => $propertyDefinition) {
26
      if ($propertyDefinition->getDataType() == 'map') {
27
        // TODO Is it possible to get the keys of a map (eg. the options array for link field) here?
28
        continue;
29
      }
30
31
      $derivatives["$entityTypeId-$fieldName-$property"] = [
32
        'name' => StringHelper::propCase($property),
33
        'property' => $property,
34
        'multi' => FALSE,
35
        'type' => $this->typeMapper->typedDataToGraphQLFieldType($propertyDefinition),
36
      ] + $commonDefinition + $basePluginDefinition;
37
    }
38
39
    return $derivatives;
40
  }
41
42
}
43