You are here

protected static function FieldResolver::getDataReferencePropertyName in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/Context/FieldResolver.php \Drupal\jsonapi\Context\FieldResolver::getDataReferencePropertyName()

Determines the reference property name for the remaining unresolved parts.

Parameters

\Drupal\Core\TypedData\ComplexDataDefinitionInterface[] $candidate_definitions: A list of targeted field item definitions specified by the path.

string[] $remaining_parts: The remaining path parts.

string[] $unresolved_path_parts: The unresolved path parts.

Return value

string The reference name.

1 call to FieldResolver::getDataReferencePropertyName()
FieldResolver::resolveInternalEntityQueryPath in core/modules/jsonapi/src/Context/FieldResolver.php
Resolves external field expressions into entity query compatible paths.

File

core/modules/jsonapi/src/Context/FieldResolver.php, line 642

Class

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

Namespace

Drupal\jsonapi\Context

Code

protected static function getDataReferencePropertyName(array $candidate_definitions, array $remaining_parts, array $unresolved_path_parts) {
  $unique_reference_names = static::getAllDataReferencePropertyNames($candidate_definitions);
  if (count($unique_reference_names) > 1) {
    $choices = array_map(function ($reference_name) use ($unresolved_path_parts, $remaining_parts) {
      $prior_parts = array_slice($unresolved_path_parts, 0, count($unresolved_path_parts) - count($remaining_parts));
      return implode('.', array_merge($prior_parts, [
        $reference_name,
      ], $remaining_parts));
    }, $unique_reference_names);

    // @todo Add test coverage for this in https://www.drupal.org/project/drupal/issues/2971281
    $message = sprintf('Ambiguous path. Try one of the following: %s, in place of the given path: %s', implode(', ', $choices), implode('.', $unresolved_path_parts));
    $cacheability = (new CacheableMetadata())
      ->addCacheContexts([
      'url.query_args:filter',
      'url.query_args:sort',
    ]);
    throw new CacheableBadRequestHttpException($cacheability, $message);
  }
  return $unique_reference_names[0];
}