You are here

function lightning_api_entity_json in Lightning API 8.4

Same name and namespace in other branches
  1. 8 lightning_api.module \lightning_api_entity_json()
  2. 8.2 lightning_api.module \lightning_api_entity_json()
  3. 8.3 lightning_api.module \lightning_api_entity_json()

Determines the URL of the JSON API representation of an entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

Return value

\Drupal\Core\Url The URL to the JSON API representation of the entity.

1 call to lightning_api_entity_json()
lightning_api_entity_operation in ./lightning_api.module
Implements hook_entity_operation().

File

./lightning_api.module, line 55
Contains hook implementations for Lightning API.

Code

function lightning_api_entity_json(EntityInterface $entity) {
  $allowed = \Drupal::config('lightning_api.settings')
    ->get('entity_json');
  $uuid = $entity
    ->uuid();

  /** @var \Drupal\jsonapi\ResourceType\ResourceType $resource_type */
  $resource_type = \Drupal::service('jsonapi.resource_type.repository')
    ->get($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  if ($allowed && $uuid && $resource_type) {
    $route_name = 'jsonapi.' . $resource_type
      ->getTypeName() . '.individual';

    // JSON API routes are built dynamically per entity bundle. If for whatever
    // reason the appropriate route does not exist yet, fail silently.
    // @see lightning_api_entity_insert().
    try {
      Drupal::service('router.route_provider')
        ->getRouteByName($route_name);
      return Url::fromRoute($route_name, [
        'entity' => $uuid,
      ]);
    } catch (RouteNotFoundException $e) {

      // No worries. The route will probably be built soon, most likely during
      // the next general cache rebuild.
    }
  }
}