function ResourceTestBase::getNestedIncludePaths

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getNestedIncludePaths()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getNestedIncludePaths()
  3. 11.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getNestedIncludePaths()

Gets an array of all nested include paths to be tested.

Parameters

int $depth: (optional) The maximum depth to which included paths should be nested.

Return value

array An array of nested include paths.

1 call to ResourceTestBase::getNestedIncludePaths()
ResourceTestBase::doTestIncluded in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Tests included resources.

File

core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php, line 3471

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getNestedIncludePaths($depth = 3) {
    $get_nested_relationship_field_names = function (EntityInterface $entity, $depth, $path = "") use (&$get_nested_relationship_field_names) {
        $relationship_field_names = $this->getRelationshipFieldNames($entity);
        if ($depth > 0) {
            $paths = [];
            foreach ($relationship_field_names as $field_name) {
                $next = $path ? "{$path}.{$field_name}" : $field_name;
                $internal_field_name = $this->resourceType
                    ->getInternalName($field_name);
                if ($target_entity = $entity->{$internal_field_name}->entity) {
                    $deep = $get_nested_relationship_field_names($target_entity, $depth - 1, $next);
                    $paths = array_merge($paths, $deep);
                }
                else {
                    $paths[] = $next;
                }
            }
            return $paths;
        }
        return array_map(function ($target_name) use ($path) {
            return "{$path}.{$target_name}";
        }, $relationship_field_names);
    };
    return $get_nested_relationship_field_names($this->entity, $depth);
}

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