You are here

public function MutableResourceTypeLinkProviderDeriver::getDerivativeDefinitions in JSON:API Hypermedia 8

Gets the definition of all derivatives of a base plugin.

Parameters

array $base_plugin_definition: The definition array of the base plugin.

Return value

array An array of full derivative definitions keyed on derivative id.

Overrides DeriverBase::getDerivativeDefinitions

See also

getDerivativeDefinition()

File

examples/Plugin/Derivative/MutableResourceTypeLinkProviderDeriver.php, line 45

Class

MutableResourceTypeLinkProviderDeriver
Derives plugins that add HATEOAS controls for mutable resources.

Namespace

Drupal\jsonapi_hypermedia\Plugin\Derivative

Code

public function getDerivativeDefinitions($base_plugin_definition) {
  $resource_types = array_filter($this->resourceTypeRepository
    ->all(), function (ResourceType $resource_type) {
    return $resource_type
      ->isLocatable() && $resource_type
      ->isMutable();
  });
  $derivative_definitions = array_reduce($resource_types, function ($derivative_definitions, ResourceType $resource_type) use ($base_plugin_definition) {
    foreach ([
      'update',
      'remove',
    ] as $operation) {
      $derivative_id = "{$resource_type->getTypeName()}.{$operation}";
      $derivative_definitions[$derivative_id] = array_merge($base_plugin_definition, [
        'link_key' => 'self',
        'link_relation_type' => $operation,
        'link_context' => [
          'resource_object' => $resource_type
            ->getTypeName(),
        ],
        'default_configuration' => [
          'operation' => $operation,
        ],
      ]);
    }
    return $derivative_definitions;
  }, []);
  return $derivative_definitions;
}