You are here

protected function LinkProviderManager::getApplicableDefinitions in JSON:API Hypermedia 8

Gets the link provider definitions applicable to the given context object.

Parameters

\Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel|\Drupal\jsonapi\JsonApiResource\ResourceObject $context: The link context object.

Return value

array An array of the application provider definitions.

See also

\Drupal\Component\Plugin\PluginManagerInterface::getDefinitions()

1 call to LinkProviderManager::getApplicableDefinitions()
LinkProviderManager::getLinkCollection in src/Plugin/LinkProviderManager.php
Gets a LinkCollection of 3rd-party links for the given context object.

File

src/Plugin/LinkProviderManager.php, line 224

Class

LinkProviderManager
Manages discovery and instantiation of resourceFieldEnhancer plugins.

Namespace

Drupal\jsonapi_hypermedia\Plugin

Code

protected function getApplicableDefinitions($context) {
  $context_type = static::getContextType($context);
  $definitions = $this
    ->getDefinitions();
  return array_filter($definitions, function ($plugin_definition) use ($context_type, $context) {
    if (!empty($plugin_definition['link_context']['relationship_object'])) {
      $plugin_definition['link_context']['top_level_object'] = 'relationship_object';
    }
    if (empty($plugin_definition['link_context'][$context_type])) {
      return FALSE;
    }
    if ($plugin_definition['link_context'][$context_type] === TRUE) {
      return TRUE;
    }
    switch ($context_type) {
      case 'top_level_object':
        assert($context instanceof JsonApiDocumentTopLevel);
        $data = $context
          ->getData();
        $is_error_document = $data instanceof ErrorCollection;
        $is_entrypoint = $this->currentRouteMatch
          ->getRouteName() === 'jsonapi.resource_list';
        $is_relationship_document = $data instanceof RelationshipData;
        switch ($plugin_definition['link_context'][$context_type]) {
          case 'entrypoint':
            return !$is_error_document && $is_entrypoint;
          case 'success':
            return !$is_error_document;
          case 'error':
            return $is_error_document;
          case 'individual':
            return !$is_error_document && !$is_entrypoint && $data instanceof ResourceObjectData && $data
              ->getCardinality() === 1;
          case 'collection':
            return !$is_error_document && !$is_entrypoint && $data instanceof ResourceObjectData && $data
              ->getCardinality() !== 1;
          case 'relationship':
            return !$is_error_document && !$is_entrypoint && $is_relationship_document;
          case 'relationship_object':
            if ($is_error_document || $is_entrypoint || !$is_relationship_document) {
              return FALSE;
            }
            if (is_bool($plugin_definition['link_context']['relationship_object'])) {
              return $plugin_definition['link_context']['relationship_object'];
            }
            $route = $this->currentRouteMatch
              ->getRouteObject();
            list($resource_type_name, $relationship_field_name) = $plugin_definition['link_context']['relationship_object'];
            return $route
              ->getDefault(Routes::RESOURCE_TYPE_KEY) === $resource_type_name && $route
              ->getDefault('related') === $relationship_field_name;
          default:
            return FALSE;
        }
      case 'resource_object':
        assert($context instanceof ResourceObject);
        return $context
          ->getResourceType()
          ->getTypeName() === $plugin_definition['link_context'][$context_type];
      case 'relationship_object':
        assert($context instanceof Relationship);
        list($resource_type_name, $relationship_field_name) = $plugin_definition['link_context'][$context_type];
        return $context
          ->getContext()
          ->getResourceType()
          ->getTypeName() === $resource_type_name && $context
          ->getFieldName() === $relationship_field_name;
      default:
        return FALSE;
    }
  });
}