You are here

public function RestExport::collectRoutes in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/rest/src/Plugin/views/display/RestExport.php \Drupal\rest\Plugin\views\display\RestExport::collectRoutes()
  2. 10 core/modules/rest/src/Plugin/views/display/RestExport.php \Drupal\rest\Plugin\views\display\RestExport::collectRoutes()

Adds the route entry of a view to the collection.

Parameters

\Symfony\Component\Routing\RouteCollection $collection: A collection of routes that should be registered for this resource.

Overrides PathPluginBase::collectRoutes

File

core/modules/rest/src/Plugin/views/display/RestExport.php, line 358

Class

RestExport
The plugin that handles Data response callbacks for REST resources.

Namespace

Drupal\rest\Plugin\views\display

Code

public function collectRoutes(RouteCollection $collection) {
  parent::collectRoutes($collection);
  $view_id = $this->view->storage
    ->id();
  $display_id = $this->display['id'];
  if ($route = $collection
    ->get("view.{$view_id}.{$display_id}")) {
    $style_plugin = $this
      ->getPlugin('style');

    // REST exports should only respond to GET methods.
    $route
      ->setMethods([
      'GET',
    ]);
    $formats = $style_plugin
      ->getFormats();

    // If there are no configured formats, add all formats that serialization
    // is known to support.
    if (!$formats) {
      $formats = $this
        ->getFormatOptions();
    }

    // Format as a string using pipes as a delimiter.
    $route
      ->setRequirement('_format', implode('|', $formats));

    // Add authentication to the route if it was set. If no authentication was
    // set, the default authentication will be used, which is cookie based by
    // default.
    $auth = $this
      ->getOption('auth');
    if (!empty($auth)) {
      $route
        ->setOption('_auth', $auth);
    }
  }
}