You are here

protected function FieldResolver::collectResourceTypesForReference in JSON:API 8

Same name and namespace in other branches
  1. 8.2 src/Context/FieldResolver.php \Drupal\jsonapi\Context\FieldResolver::collectResourceTypesForReference()

Build a list of resource types depending on which bundles are referenced.

@todo Add PHP type hint, see https://www.drupal.org/project/jsonapi/issues/2933895

Parameters

\Drupal\Core\Field\TypedData\FieldItemDataDefinition $item_definition: The reference definition.

Return value

\Drupal\jsonapi\ResourceType\ResourceType[] The list of resource types.

1 call to FieldResolver::collectResourceTypesForReference()
FieldResolver::getReferenceableResourceTypes in src/Context/FieldResolver.php
Get the referenceable ResourceTypes for a set of field definitions.

File

src/Context/FieldResolver.php, line 466

Class

FieldResolver
A service that evaluates external path expressions against Drupal fields.

Namespace

Drupal\jsonapi\Context

Code

protected function collectResourceTypesForReference(FieldItemDataDefinition $item_definition) {
  $main_property_definition = $item_definition
    ->getPropertyDefinition($item_definition
    ->getMainPropertyName());

  // Check if the field is a flavor of an Entity Reference field.
  if (!$main_property_definition instanceof DataReferenceTargetDefinition) {
    return [];
  }
  $entity_type_id = $item_definition
    ->getSetting('target_type');
  $handler_settings = $item_definition
    ->getSetting('handler_settings');
  $has_target_bundles = isset($handler_settings['target_bundles']) && !empty($handler_settings['target_bundles']);
  $target_bundles = $has_target_bundles ? $handler_settings['target_bundles'] : $this
    ->getAllBundlesForEntityType($entity_type_id);
  return array_map(function ($bundle) use ($entity_type_id) {
    return $this->resourceTypeRepository
      ->get($entity_type_id, $bundle);
  }, $target_bundles);
}