Completed
Push — 8.x-3.x ( 038dc8...7d2b82 )
by Philipp
01:33
created

getDerivativeDefinitionsFromFieldDefinition()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 2
dl 0
loc 22
rs 8.9457
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\graphql\Utility\StringHelper;
7
use Drupal\graphql_core\Plugin\Deriver\EntityFieldDeriverBase;
8
9
class EntityFieldPropertyDeriver extends EntityFieldDeriverBase {
10
11
  /**
12
   * {@inheritdoc}
13
   */
14
  protected function getDerivativeDefinitionsFromFieldDefinition(FieldDefinitionInterface $fieldDefinition, array $basePluginDefinition) {
15
    $fieldType = $fieldDefinition->getType();
16
17
    if (isset($basePluginDefinition['field_types']) && in_array($fieldType, $basePluginDefinition['field_types'])) {
18
      $fieldName = $fieldDefinition->getName();
19
      $fieldBundle = $fieldDefinition->getTargetBundle() ?: '';
20
      $entityTypeId = $fieldDefinition->getTargetEntityTypeId();
21
      $entityType = $this->entityTypeManager->getDefinition($entityTypeId);
22
      $supportsBundles = $entityType->hasKey('bundle');
23
24
      if (!isset($basePluginDefinition['parents'])) {
25
        $basePluginDefinition['parents'] = [];
26
      }
27
28
      $parents = [StringHelper::camelCase('field', $entityTypeId, $supportsBundles ? $fieldBundle : '', $fieldName)];
29
      return ["$entityTypeId-$fieldBundle-$fieldName" => [
30
        'parents' => array_merge($parents, $basePluginDefinition['parents']),
31
      ] + $basePluginDefinition];
32
    }
33
34
    return [];
35
  }
36
}
37