You are here

public function EntityReferenceQueryDeriver::getDerivativeDefinitions in GraphQL 8.3

Gets the definition of all derivatives of a base plugin.

Parameters

array $base_plugin_definition: The definition array of the base plugin.

Return value

array An array of full derivative definitions keyed on derivative id.

Overrides DeriverBase::getDerivativeDefinitions

See also

getDerivativeDefinition()

File

modules/graphql_core/src/Plugin/Deriver/Fields/EntityReferenceQueryDeriver.php, line 74

Class

EntityReferenceQueryDeriver

Namespace

Drupal\graphql_core\Plugin\Deriver\Fields

Code

public function getDerivativeDefinitions($basePluginDefinition) {
  $fieldMap = $this->entityFieldManager
    ->getFieldMap();
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entityTypeId => $entityType) {
    $interfaces = class_implements($entityType
      ->getClass());
    if (!array_key_exists(FieldableEntityInterface::class, $interfaces)) {
      continue;
    }
    foreach ($this->entityFieldManager
      ->getFieldStorageDefinitions($entityTypeId) as $fieldDefinition) {
      if ($fieldDefinition
        ->getType() !== 'entity_reference' || !($targetTypeId = $fieldDefinition
        ->getSetting('target_type'))) {
        continue;
      }
      $tags = array_merge($fieldDefinition
        ->getCacheTags(), [
        'entity_field_info',
      ]);
      $contexts = $fieldDefinition
        ->getCacheContexts();
      $maxAge = $fieldDefinition
        ->getCacheMaxAge();
      $targetType = $this->entityTypeManager
        ->getDefinition($targetTypeId);
      $fieldName = $fieldDefinition
        ->getName();
      if ($fieldDefinition instanceof BaseFieldDefinition || !$entityType
        ->hasKey('bundle')) {
        $parents = [
          StringHelper::camelCase($entityTypeId),
        ];
      }
      else {
        $parents = [];
        foreach ($fieldMap[$entityTypeId][$fieldName]['bundles'] as $bundle) {
          $parents[] = StringHelper::camelCase($entityTypeId . '_' . $bundle);
        }
      }
      $derivative = [
        'parents' => $parents,
        'name' => StringHelper::propCase('query', $fieldName),
        'description' => $this
          ->t('Query reference: @description', [
          '@description' => $fieldDefinition
            ->getDescription(),
        ]),
        'field' => $fieldName,
        'entity_key' => $targetType
          ->getKey('id'),
        'entity_type' => $targetTypeId,
        'schema_cache_tags' => $tags,
        'schema_cache_contexts' => $contexts,
        'schema_cache_max_age' => $maxAge,
      ] + $basePluginDefinition;

      /** @var \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface $definition */
      $this->derivatives["{$entityTypeId}-{$fieldName}"] = $derivative;
    }
  }
  return $this->derivatives;
}