Completed
Pull Request — 8.x-3.x (#684)
by Sebastian
13:11
created

EntityFieldDeriver::getParentsForField()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
4
5
use Drupal\Core\Field\FieldDefinitionInterface;
6
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
7
use Drupal\Core\TypedData\ListDataDefinitionInterface;
8
use Drupal\graphql\Utility\StringHelper;
9
use Drupal\graphql_core\Plugin\Deriver\EntityFieldDeriverBase;
10
11
class EntityFieldDeriver extends EntityFieldDeriverBase {
12
13
  /**
14
   * {@inheritdoc}
15
   */
16
  protected function getDerivativeDefinitionsFromFieldDefinition(FieldDefinitionInterface $fieldDefinition, array $basePluginDefinition) {
17
    $itemDefinition = $fieldDefinition->getItemDefinition();
18
    if (!($itemDefinition instanceof ComplexDataDefinitionInterface) || !$propertyDefinitions = $itemDefinition->getPropertyDefinitions()) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\TypedData\Co...DataDefinitionInterface 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...
19
      return [];
20
    }
21
22
    $tags = array_merge($fieldDefinition->getCacheTags(), ['entity_field_info']);
23
    $maxAge = $fieldDefinition->getCacheMaxAge();
24
    $contexts = $fieldDefinition->getCacheContexts();
25
26
    $entityTypeId = $fieldDefinition->getTargetEntityTypeId();
27
    $entityType = $this->entityTypeManager->getDefinition($entityTypeId);
28
    $supportsBundles = $entityType->hasKey('bundle');
29
    $fieldName = $fieldDefinition->getName();
30
    $fieldBundle = $fieldDefinition->getTargetBundle() ?: '';
31
32
    $derivative = [
33
      'parents' => [StringHelper::camelCase($entityTypeId, $supportsBundles ? $fieldBundle : '')],
34
      'name' => StringHelper::propCase($fieldName),
35
      'description' => $fieldDefinition->getDescription(),
36
      'field' => $fieldName,
37
      'schema_cache_tags' => $tags,
38
      'schema_cache_contexts' => $contexts,
39
      'schema_cache_max_age' => $maxAge,
40
    ] + $basePluginDefinition;
41
42
    if (count($propertyDefinitions) === 1) {
43
      $propertyDefinition = reset($propertyDefinitions);
44
      $derivative['type'] = $propertyDefinition->getDataType();
45
      $derivative['property'] = key($propertyDefinitions);
46
    }
47
    else {
48
      $derivative['type'] = StringHelper::camelCase('field', $entityTypeId, $supportsBundles ? $fieldBundle : '', $fieldName);
49
    }
50
51
    // Fields are usually multi-value. Simplify them for the schema if they are
52
    // configured for cardinality 1 (only works for configured fields).
53
    if (!(($storageDefinition = $fieldDefinition->getFieldStorageDefinition()) && !$storageDefinition->isMultiple())) {
54
      $derivative['type'] = StringHelper::listType($derivative['type']);
55
    }
56
57
    return ["$entityTypeId-$fieldName-$fieldBundle" => $derivative];
58
  }
59
}
60