You are here

public function EntityPublishedInterfaceLinkProviderDeriver::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/EntityPublishedInterfaceLinkProviderDeriver.php, line 60

Class

EntityPublishedInterfaceLinkProviderDeriver
Class EntityPublishedInterfaceLinkProviderDeriver.

Namespace

Drupal\jsonapi_hypermedia\Plugin\Derivative

Code

public function getDerivativeDefinitions($base_plugin_definition) {
  $entity_types = $this->entityTypeManager
    ->getDefinitions();
  $resource_types = array_filter($this->resourceTypeRepository
    ->all(), function (ResourceType $resource_type) use ($entity_types) {
    $entity_type_id = $resource_type
      ->getEntityTypeId();
    return $resource_type
      ->isLocatable() && $resource_type
      ->isMutable() && isset($entity_types[$entity_type_id]) && $entity_types[$entity_type_id]
      ->entityClassImplements(EntityPublishedInterface::class);
  });
  $derivative_definitions = array_reduce($resource_types, function ($derivative_definitions, ResourceType $resource_type) use ($base_plugin_definition, $entity_types) {
    foreach ([
      'publish',
      'unpublish',
    ] as $operation) {
      $derivative_id = "{$resource_type->getTypeName()}.{$operation}";
      $derivative_definitions[$derivative_id] = array_merge($base_plugin_definition, [
        'link_key' => $operation,
        'link_relation_type' => 'update',
        'link_context' => [
          'resource_object' => $resource_type
            ->getTypeName(),
        ],
        'default_configuration' => [
          'status_field_name' => $entity_types[$resource_type
            ->getEntityTypeId()]
            ->getKey('published'),
        ],
      ]);
    }
    return $derivative_definitions;
  }, []);
  return $derivative_definitions;
}