You are here

public function ResourceRoutes::routes in Acquia Content Hub 8

Generates Content Hub REST resource routes for every eligible entity type.

File

src/Routing/ResourceRoutes.php, line 45

Class

ResourceRoutes
Defines Acquia Content Hub Dynamic REST routes.

Namespace

Drupal\acquia_contenthub\Routing

Code

public function routes() {
  $collection = new RouteCollection();
  $entity_type_ids = $this->entityManager
    ->getContentHubEnabledEntityTypeIds();
  foreach ($entity_type_ids as $entity_type_id) {

    // Match the behavior of \Drupal\rest\Plugin\rest\resource\EntityResource:
    // use the entity type's canonical link template if it has one, otherwise
    // use EntityResource's generic alternative.
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    $canonical_path = $entity_type
      ->hasLinkTemplate('canonical') ? $entity_type
      ->getLinkTemplate('canonical') : '/entity/' . $entity_type_id . '/{' . $entity_type_id . '}';
    $content_hub_path = '/acquia-contenthub-cdf' . $canonical_path;
    $route = new Route($content_hub_path, [
      '_controller' => '\\Drupal\\acquia_contenthub\\Controller\\ContentHubEntityRequestHandler::handle',
      // @see \Drupal\acquia_contenthub\Controller\ContentHubEntityRequestHandler
      // @todo Remove this when https://www.drupal.org/node/2822201 lands, and this module is able to require Drupal 8.3.x.
      '_acquia_content_hub_rest_resource_plugin_id' => 'entity:' . $entity_type_id,
    ]);
    $route
      ->setOption('parameters', [
      $entity_type_id => [
        'type' => 'entity:' . $entity_type_id,
      ],
    ]);

    // Only allow the Acquia Content Hub CDF format.
    $route
      ->setRequirement('_format', 'acquia_contenthub_cdf|html|json');

    // Only allow access to the CDF if the request is coming from a logged
    // in user with 'Administer Acquia Content Hub' permission or if it
    // is coming from Acquia Content Hub (validates the HMAC signature).
    $route
      ->setRequirement('_contenthub_access', 'TRUE');

    // Only allow GET.
    $route
      ->setMethods([
      'GET',
    ]);
    $collection
      ->add('acquia_contenthub.entity.' . $entity_type_id . '.GET.acquia_contenthub_cdf', $route);
  }
  return $collection;
}