You are here

protected function ResourceTypeRepository::calculateRelatableResourceTypes in JSON:API 8.2

Same name and namespace in other branches
  1. 8 src/ResourceType/ResourceTypeRepository.php \Drupal\jsonapi\ResourceType\ResourceTypeRepository::calculateRelatableResourceTypes()

Calculates relatable JSON:API resource types for a given resource type.

This method has no affect after being called once.

Parameters

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The resource type repository.

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

Return value

array The relatable JSON:API resource types, keyed by field name.

1 call to ResourceTypeRepository::calculateRelatableResourceTypes()
ResourceTypeRepository::all in src/ResourceType/ResourceTypeRepository.php
Gets all JSON:API resource types.

File

src/ResourceType/ResourceTypeRepository.php, line 349

Class

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

Namespace

Drupal\jsonapi\ResourceType

Code

protected function calculateRelatableResourceTypes(ResourceType $resource_type, array $resource_types) {

  // For now, only fieldable entity types may contain relationships.
  $entity_type = $this->entityTypeManager
    ->getDefinition($resource_type
    ->getEntityTypeId());
  if ($entity_type
    ->entityClassImplements(FieldableEntityInterface::class)) {
    $field_definitions = $this->entityFieldManager
      ->getFieldDefinitions($resource_type
      ->getEntityTypeId(), $resource_type
      ->getBundle());
    $relatable_internal = array_map(function ($field_definition) use ($resource_types) {
      return $this
        ->getRelatableResourceTypesFromFieldDefinition($field_definition, $resource_types);
    }, array_filter($field_definitions, function ($field_definition) {
      return $this
        ->isReferenceFieldDefinition($field_definition);
    }));
    $relatable_public = [];
    foreach ($relatable_internal as $internal_field_name => $value) {
      $relatable_public[$resource_type
        ->getPublicName($internal_field_name)] = $value;
    }
    return $relatable_public;
  }
  return [];
}