function ResourceType::setRelatableResourceTypes

Same name in other branches
  1. 9 core/modules/jsonapi/src/ResourceType/ResourceType.php \Drupal\jsonapi\ResourceType\ResourceType::setRelatableResourceTypes()
  2. 10 core/modules/jsonapi/src/ResourceType/ResourceType.php \Drupal\jsonapi\ResourceType\ResourceType::setRelatableResourceTypes()
  3. 11.x core/modules/jsonapi/src/ResourceType/ResourceType.php \Drupal\jsonapi\ResourceType\ResourceType::setRelatableResourceTypes()

Sets the relatable resource types.

Parameters

array $relatable_resource_types: The resource types with which this resource type may have a relationship. The array should be a multi-dimensional array keyed by public field name whose values are an array of resource types. There may be duplicate across resource types across fields, but not within a field.

File

core/modules/jsonapi/src/ResourceType/ResourceType.php, line 377

Class

ResourceType
Value object containing all metadata for a JSON:API resource type.

Namespace

Drupal\jsonapi\ResourceType

Code

public function setRelatableResourceTypes(array $relatable_resource_types) {
    $this->fields = array_reduce(array_keys($relatable_resource_types), function ($fields, $public_field_name) use ($relatable_resource_types) {
        if (!isset($this->fieldMapping[$public_field_name])) {
            throw new \LogicException('A field must exist for relatable resource types to be set on it.');
        }
        $internal_field_name = $this->fieldMapping[$public_field_name];
        $field = $fields[$internal_field_name];
        assert($field instanceof ResourceTypeRelationship);
        $fields[$internal_field_name] = $field->withRelatableResourceTypes($relatable_resource_types[$public_field_name]);
        return $fields;
    }, $this->fields);
}

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