You are here

function lightning_api_entity_json in Lightning API 8

Same name and namespace in other branches
  1. 8.4 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()
1 call to lightning_api_entity_json()
lightning_api_entity_operation in ./lightning_api.module
Implements hook_entity_operation().

File

./lightning_api.module, line 37

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 = '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);
      return Url::fromRoute($route, [
        $resource_type
          ->getEntityTypeId() => $uuid,
      ]);
    } catch (RouteNotFoundException $e) {

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