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

getDerivativeDefinitionsFromFieldDefinition()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 3
dl 0
loc 34
c 0
b 0
f 0
cc 3
eloc 22
nop 3
rs 8.8571
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
4
5
use Drupal\Core\Field\FieldConfigInterface;
6
use Drupal\Core\Field\FieldStorageDefinitionInterface;
7
use Drupal\field\FieldStorageConfigInterface;
8
use Drupal\graphql\Utility\StringHelper;
9
use Drupal\graphql_core\Plugin\Deriver\EntityFieldDeriverWithTypeMapping;
10
use Drupal\graphql_core\Plugin\GraphQL\Fields\Entity\EntityField;
11
use Drupal\graphql_core\Plugin\GraphQL\Types\Entity\EntityFieldType;
12
13
// TODO Write tests for entity reference graph traversal.
14
15
// TODO Should we expose config entities?
16
17
// TODO Convert timestamps to strings?
18
19
/**
20
 * Deriver for RawValue fields.
21
 */
22
class EntityFieldDeriver extends EntityFieldDeriverWithTypeMapping {
23
24
  /**
25
   * {@inheritdoc}
26
   */
27
  protected function getDerivativeDefinitionsFromFieldDefinition($entityTypeId, FieldStorageDefinitionInterface $fieldDefinition, array $basePluginDefinition) {
28
    $fieldName = $fieldDefinition->getName();
29
    if (!$parents = $this->getParentsForField($entityTypeId, $fieldDefinition)) {
30
      return [];
31
    }
32
33
    $derivative = [
34
      'parents' => $parents,
35
      'name' => EntityField::getId($fieldName),
36
      'multi' => $fieldDefinition->isMultiple(),
37
      'field' => $fieldName,
38
      'schema_cache_tags' => array_merge($fieldDefinition->getCacheTags(), ['entity_field_info']),
39
      'schema_cache_contexts' => $fieldDefinition->getCacheContexts(),
40
      'schema_cache_max_age' => $fieldDefinition->getCacheMaxAge(),
41
    ];
42
43
    $properties = $fieldDefinition->getPropertyDefinitions();
44
    if (count($properties) === 1) {
45
      // Flatten the structure for single-property fields.
46
      /** @var \Drupal\Core\TypedData\DataDefinitionInterface $property */
47
      $property = reset($properties);
48
      $keys = array_keys($properties);
49
50
      $derivative['type'] = $this->typeMapper->typedDataToGraphQLFieldType($property);
51
      $derivative['property'] = reset($keys);
52
    }
53
    else {
54
      $derivative['type'] = EntityFieldType::getId($entityTypeId, $fieldName);
55
    }
56
57
    return [
58
      "$entityTypeId-$fieldName" => $derivative + $basePluginDefinition,
59
    ];
60
  }
61
62
  protected function getParentsForField($entityTypeId, FieldStorageDefinitionInterface $fieldDefinition) {
63
    if ($fieldDefinition->isBaseField()) {
64
      return [StringHelper::camelCase([$entityTypeId])];
65
    }
66
67
    if ($fieldDefinition instanceof FieldStorageConfigInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\field\FieldStorageConfigInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
68
      return array_values(array_map(function ($bundleId) use ($entityTypeId) {
69
        return [StringHelper::camelCase([$entityTypeId, $bundleId])];
70
      }, $fieldDefinition->getBundles()));
71
    }
72
73
    return [];
74
  }
75
}
76