function IncludeResolver::toIncludeTree

Same name in other branches
  1. 9 core/modules/jsonapi/src/IncludeResolver.php \Drupal\jsonapi\IncludeResolver::toIncludeTree()
  2. 8.9.x core/modules/jsonapi/src/IncludeResolver.php \Drupal\jsonapi\IncludeResolver::toIncludeTree()
  3. 11.x core/modules/jsonapi/src/IncludeResolver.php \Drupal\jsonapi\IncludeResolver::toIncludeTree()

Returns a tree of field names to include from an include parameter.

Parameters

\Drupal\jsonapi\JsonApiResource\ResourceObjectData $data: The base resources for which includes should be resolved.

string $include_parameter: The raw include parameter value.

Return value

array A multi-dimensional array representing a tree of field names to be included. Array keys are the field names. Leaves are empty arrays.

1 call to IncludeResolver::toIncludeTree()
IncludeResolver::resolve in core/modules/jsonapi/src/IncludeResolver.php
Resolves included resources.

File

core/modules/jsonapi/src/IncludeResolver.php, line 196

Class

IncludeResolver
Resolves included resources for an entity or collection of entities.

Namespace

Drupal\jsonapi

Code

protected static function toIncludeTree(ResourceObjectData $data, $include_parameter) {
    // $include_parameter: 'one.two.three, one.two.four'.
    $include_paths = array_map('trim', explode(',', $include_parameter));
    // $exploded_paths: [['one', 'two', 'three'], ['one', 'two', 'four']].
    $exploded_paths = array_map(function ($include_path) {
        return array_map('trim', explode('.', $include_path));
    }, $include_paths);
    $resolved_paths_per_resource_type = [];
    
    /** @var \Drupal\jsonapi\JsonApiResource\ResourceIdentifierInterface $resource_object */
    foreach ($data as $resource_object) {
        $resource_type = $resource_object->getResourceType();
        $resource_type_name = $resource_type->getTypeName();
        if (isset($resolved_paths_per_resource_type[$resource_type_name])) {
            continue;
        }
        $resolved_paths_per_resource_type[$resource_type_name] = static::resolveInternalIncludePaths($resource_type, $exploded_paths);
    }
    $resolved_paths = array_reduce($resolved_paths_per_resource_type, 'array_merge', []);
    return static::buildTree($resolved_paths);
}

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