function ResourceTypeRepository::getRelatableResourceTypesFromFieldDefinition

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/ResourceType/ResourceTypeRepository.php \Drupal\jsonapi\ResourceType\ResourceTypeRepository::getRelatableResourceTypesFromFieldDefinition()
  2. 8.9.x core/modules/jsonapi/src/ResourceType/ResourceTypeRepository.php \Drupal\jsonapi\ResourceType\ResourceTypeRepository::getRelatableResourceTypesFromFieldDefinition()
  3. 10 core/modules/jsonapi/src/ResourceType/ResourceTypeRepository.php \Drupal\jsonapi\ResourceType\ResourceTypeRepository::getRelatableResourceTypesFromFieldDefinition()

Get relatable resource types from a field definition.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition from which to calculate relatable JSON:API resource types.

\Drupal\jsonapi\ResourceType\ResourceType[] $resource_types: A list of JSON:API resource types.

Return value

\Drupal\jsonapi\ResourceType\ResourceType[] The JSON:API resource types with which the given field may have a relationship.

1 call to ResourceTypeRepository::getRelatableResourceTypesFromFieldDefinition()
ResourceTypeRepository::calculateRelatableResourceTypes in core/modules/jsonapi/src/ResourceType/ResourceTypeRepository.php
Calculates relatable JSON:API resource types for a given resource type.

File

core/modules/jsonapi/src/ResourceType/ResourceTypeRepository.php, line 442

Class

ResourceTypeRepository
Provides a repository of all JSON:API resource types.

Namespace

Drupal\jsonapi\ResourceType

Code

protected function getRelatableResourceTypesFromFieldDefinition(FieldDefinitionInterface $field_definition, array $resource_types) {
  $item_definition = $field_definition->getItemDefinition();
  $entity_type_id = $item_definition->getSetting('target_type');
  $relatable_resource_types = [];
  $item_class = $item_definition->getClass();
  $target_type_bundles = $item_class::getReferenceableBundles($field_definition);
  foreach ($target_type_bundles as $entity_type_id => $target_bundles) {
    foreach ($target_bundles as $target_bundle) {
      if ($resource_type = static::lookupResourceType($resource_types, $entity_type_id, $target_bundle)) {
        $relatable_resource_types[] = $resource_type;
        continue;
      }
      // Do not warn during site installation since system integrity
      // is not guaranteed during this period and may cause confusing and
      // unnecessary warnings.
      if (!InstallerKernel::installationAttempted()) {
        $this->getLogger('jsonapi')
          ->warning('The "@name" at "@target_entity_type_id:@target_bundle" references the "@entity_type_id:@bundle" entity type that does not exist.', [
          '@name' => $field_definition->getName(),
          '@target_entity_type_id' => $field_definition->getTargetEntityTypeId(),
          '@target_bundle' => $field_definition->getTargetBundle(),
          '@entity_type_id' => $entity_type_id,
          '@bundle' => $target_bundle,
        ]);
      }
    }
  }
  return $relatable_resource_types;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.