Completed
Pull Request — 8.x-3.x (#571)
by Philipp
02:54
created

EntityReferenceReverseDeriver::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
4
5
use Drupal\Component\Plugin\Derivative\DeriverBase;
6
use Drupal\Core\Entity\EntityFieldManagerInterface;
7
use Drupal\Core\Entity\EntityTypeManagerInterface;
8
use Drupal\Core\Entity\FieldableEntityInterface;
9
use Drupal\Core\Field\BaseFieldDefinition;
10
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
11
use Drupal\Core\StringTranslation\StringTranslationTrait;
12
use Drupal\Core\TypedData\TypedDataManagerInterface;
13
use Drupal\graphql\Utility\StringHelper;
14
use Drupal\graphql_core\Plugin\Deriver\EntityTypeDeriverBase;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
class EntityReferenceReverseDeriver extends DeriverBase implements ContainerDeriverInterface {
18
  use StringTranslationTrait;
19
20
  /**
21
   * The entity type manager.
22
   *
23
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
24
   */
25
  protected $entityTypeManager;
26
27
  /**
28
   * The entity field manager.
29
   *
30
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
31
   */
32
  protected $entityFieldManager;
33
34
  /**
35
   * The typed data manager service.
36
   *
37
   * @var \Drupal\Core\TypedData\TypedDataManagerInterface
38
   */
39
  protected $typedDataManager;
40
41
  /**
42
   * {@inheritdoc}
43
   */
44
  public static function create(ContainerInterface $container, $basePluginId) {
45
    return new static(
46
      $container->get('entity_type.manager'),
47
      $container->get('entity_field.manager'),
48
      $container->get('typed_data_manager')
49
    );
50
  }
51
52
  /**
53
   * RawValueFieldItemDeriver constructor.
54
   *
55
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
56
   *   The entity type manager.
57
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
58
   *   The entity field manager.
59
   * @param \Drupal\Core\TypedData\TypedDataManagerInterface $typedDataManager
60
   *   The typed data manager service.
61
   */
62
  public function __construct(
63
    EntityTypeManagerInterface $entityTypeManager,
64
    EntityFieldManagerInterface $entityFieldManager,
65
    TypedDataManagerInterface $typedDataManager
66
  ) {
67
    $this->entityTypeManager = $entityTypeManager;
68
    $this->entityFieldManager = $entityFieldManager;
69
    $this->typedDataManager = $typedDataManager;
70
  }
71
72
  /**
73
   * {@inheritdoc}
74
   */
75
  public function getDerivativeDefinitions($basePluginDefinition) {
76
    foreach ($this->entityTypeManager->getDefinitions() as $entityTypeId => $entityType) {
77
      $interfaces = class_implements($entityType->getClass());
78
      if (!array_key_exists(FieldableEntityInterface::class, $interfaces) || !EntityTypeDeriverBase::isAccessibleEntityType($entityType)) {
79
        continue;
80
      }
81
82
      foreach ($this->entityFieldManager->getFieldStorageDefinitions($entityTypeId) as $fieldDefinition) {
83
        if ($fieldDefinition->getType() !== 'entity_reference' || !$targetTypeId = $fieldDefinition->getSetting('target_type')) {
84
          continue;
85
        }
86
87
        $tags = array_merge($fieldDefinition->getCacheTags(), ['entity_field_info']);
88
        $contexts = $fieldDefinition->getCacheContexts();
89
        $maxAge = $fieldDefinition->getCacheMaxAge();
90
91
        $fieldName = $fieldDefinition->getName();
92
        $derivative = [
93
          'parents' => [StringHelper::camelCase($targetTypeId)],
94
          'name' => StringHelper::propCase('reverse', $fieldName, $entityTypeId),
95
          'description' => $this->t('Reverse reference: @description', [
96
            '@description' => $fieldDefinition->getDescription(),
97
          ]),
98
          'field' => $fieldName,
99
          'entity_type' => $entityTypeId,
100
          'schema_cache_tags' => $tags,
101
          'schema_cache_contexts' => $contexts,
102
          'schema_cache_max_age' => $maxAge,
103
          'response_cache_tags' => $tags,
104
          'response_cache_contexts' => $contexts,
105
          'response_cache_max_age' => $maxAge,
106
        ] + $basePluginDefinition;
107
108
        /** @var \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface $definition */
109
        $this->derivatives["$entityTypeId-$fieldName"] = $derivative;
110
      }
111
    }
112
113
    return $this->derivatives;
114
  }
115
}
116