function IncludeResolver::buildTree

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

Takes an array of exploded paths and builds a tree of field names.

Input example: [ ['one', 'two', 'three'], ['one', 'two', 'four'], ['one', 'two', 'internal'], ]

Output example: [ 'one' => [ 'two' [ 'three' => [], 'four' => [], 'internal' => [], ], ], ]

Parameters

array $paths: An array of exploded include paths.

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::buildTree()
IncludeResolver::toIncludeTree in core/modules/jsonapi/src/IncludeResolver.php
Returns a tree of field names to include from an include parameter.

File

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

Class

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

Namespace

Drupal\jsonapi

Code

protected static function buildTree(array $paths) {
    $merged = [];
    foreach ($paths as $parts) {
        if (!($field_name = array_shift($parts))) {
            continue;
        }
        $previous = $merged[$field_name] ?? [];
        $merged[$field_name] = array_merge($previous, [
            $parts,
        ]);
    }
    return !empty($merged) ? array_map([
        static::class,
        __FUNCTION__,
    ], $merged) : $merged;
}

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