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

EntityFieldDeriverBase::getDerivativeDefinitions()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 23

Duplication

Lines 10
Ratio 43.48 %

Importance

Changes 0
Metric Value
cc 8
nc 14
nop 1
dl 10
loc 23
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver;
4
5
use Drupal\Component\Plugin\Derivative\DeriverBase;
6
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
7
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8
use Drupal\Core\Entity\EntityTypeManagerInterface;
9
use Drupal\Core\Entity\FieldableEntityInterface;
10
use Drupal\Core\Field\FieldDefinitionInterface;
11
use Drupal\Core\Field\FieldStorageDefinitionInterface;
12
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
13
use Drupal\Core\Entity\EntityFieldManagerInterface;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
16
/**
17
 * Generate GraphQLField plugins for config fields.
18
 */
19
abstract class EntityFieldDeriverBase extends DeriverBase implements ContainerDeriverInterface {
20
  use DependencySerializationTrait;
21
22
  /**
23
   * Provides plugin definition values from fields.
24
   *
25
   * @param \Drupal\Core\Field\FieldDefinitionInterface $fieldDefinition
26
   *   Field definition object.
27
   * @param array $basePluginDefinition
28
   *   Base definition array.
29
   *
30
   * @return array
31
   *   The derived plugin definitions for the given field.
32
   */
33
  abstract protected function getDerivativeDefinitionsFromFieldDefinition(FieldDefinitionInterface $fieldDefinition, array $basePluginDefinition);
34
35
  /**
36
   * The entity type manager.
37
   *
38
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
39
   */
40
  protected $entityTypeManager;
41
42
  /**
43
   * The entity field manager.
44
   *
45
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
46
   */
47
  protected $entityFieldManager;
48
49
  /**
50
   * The entity bundle info.
51
   *
52
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
53
   */
54
  protected $entityBundleInfo;
55
56
  /**
57
   * The base plugin id.
58
   *
59
   * @var string
60
   */
61
  protected $basePluginId;
62
63
  /**
64
   * {@inheritdoc}
65
   */
66
  public static function create(ContainerInterface $container, $basePluginId) {
67
    return new static(
68
      $container->get('entity_type.manager'),
69
      $container->get('entity_field.manager'),
70
      $container->get('entity_type.bundle.info'),
71
      $basePluginId
72
    );
73
  }
74
75
  /**
76
   * RawValueFieldItemDeriver constructor.
77
   *
78
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
79
   *   The entity type manager.
80
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
81
   *   The entity field manager.
82
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
83
   *   The bundle info service.
84
   * @param string $basePluginId
85
   *   The base plugin id.
86
   */
87
  public function __construct(
88
    EntityTypeManagerInterface $entityTypeManager,
89
    EntityFieldManagerInterface $entityFieldManager,
90
    EntityTypeBundleInfoInterface $entityTypeBundleInfo,
91
    $basePluginId
92
  ) {
93
    $this->basePluginId = $basePluginId;
94
    $this->entityTypeManager = $entityTypeManager;
95
    $this->entityFieldManager = $entityFieldManager;
96
    $this->entityBundleInfo = $entityTypeBundleInfo;
97
  }
98
99
  /**
100
   * {@inheritdoc}
101
   */
102
  public function getDerivativeDefinitions($basePluginDefinition) {
103
    foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
104
      if (!$entityType->entityClassImplements(FieldableEntityInterface::class)) {
105
        continue;
106
      }
107
108 View Code Duplication
      foreach ($this->entityFieldManager->getBaseFieldDefinitions($entityTypeId) as $fieldDefinition) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
        if ($derivatives = $this->getDerivativeDefinitionsFromFieldDefinition($fieldDefinition, $basePluginDefinition)) {
110
          $this->derivatives = array_merge($this->derivatives, $derivatives);
111
        }
112
      }
113
114
      foreach ($this->entityBundleInfo->getBundleInfo($entityTypeId) as $bundleId => $bundleInfo) {
115 View Code Duplication
        foreach ($this->entityFieldManager->getFieldDefinitions($entityTypeId, $bundleId) as $fieldDefinition) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
          if ($derivatives = $this->getDerivativeDefinitionsFromFieldDefinition($fieldDefinition, $basePluginDefinition)) {
117
            $this->derivatives = array_merge($this->derivatives, $derivatives);
118
          }
119
        }
120
      }
121
    }
122
123
    return $this->derivatives;
124
  }
125
126
}
127