public function PathPluginBase::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 DisplayRouterInterface::collectRoutes

1 call to PathPluginBase::collectRoutes()
RestExport::collectRoutes in drupal/core/modules/rest/lib/Drupal/rest/Plugin/views/display/RestExport.php
Adds the route entry of a view to the collection.
1 method overrides PathPluginBase::collectRoutes()
RestExport::collectRoutes in drupal/core/modules/rest/lib/Drupal/rest/Plugin/views/display/RestExport.php
Adds the route entry of a view to the collection.

File

drupal/core/modules/views/lib/Drupal/views/Plugin/views/display/PathPluginBase.php, line 65
Contains \Drupal\views\Plugin\views\display\PathPluginBase.

Class

PathPluginBase
The base display plugin for path/callbacks. This is used for pages and feeds.

Namespace

Drupal\views\Plugin\views\display

Code

public function collectRoutes(RouteCollection $collection) {
  $view_id = $this->view->storage
    ->id();
  $display_id = $this->display['id'];
  $defaults = array(
    '_controller' => 'Drupal\\views\\Routing\\ViewPageController::handle',
    'view_id' => $view_id,
    'display_id' => $display_id,
  );

  // @todo How do we apply argument validation?
  $bits = explode('/', $this
    ->getOption('path'));

  // @todo Figure out validation/argument loading.
  // Replace % with %views_arg for menu autoloading and add to the
  // page arguments so the argument actually comes through.
  $arg_counter = 0;
  $this->view
    ->initHandlers();
  $view_arguments = $this->view->argument;
  $argument_ids = array_keys($view_arguments);
  $total_arguments = count($argument_ids);

  // Replace arguments in the views UI (defined via %) with parameters in
  // routes (defined via {}). As a name for the parameter use arg_$key, so
  // it can be pulled in the views controller from the request.
  foreach ($bits as $pos => $bit) {
    if ($bit == '%') {

      // Generate the name of the parameter using the key of the argument
      // handler.
      $arg_id = 'arg_' . $argument_ids[$arg_counter++];
      $bits[$pos] = '{' . $arg_id . '}';
    }
  }

  // Add missing arguments not defined in the path, but added as handler.
  while ($total_arguments - $arg_counter > 0) {
    $arg_id = 'arg_' . $argument_ids[$arg_counter++];
    $bit = '{' . $arg_id . '}';

    // In contrast to the previous loop add the defaults here, as % was not
    // specified, which means the argument is optional.
    $defaults[$arg_id] = NULL;
    $bits[] = $bit;
  }

  // If this is to be a default tab, create the route for the parent path.
  if ($this
    ->isDefaultTabPath()) {
    $bit = array_pop($bits);
    if ($bit == '%views_arg' || empty($bits)) {
      $bits[] = $bit;
    }
  }
  $route_path = '/' . implode('/', $bits);
  $route = new Route($route_path, $defaults);

  // Add access check parameters to the route.
  $access_plugin = $this
    ->getPlugin('access');
  if (!isset($access_plugin)) {

    // @todo Do we want to support a default plugin in getPlugin itself?
    $access_plugin = Views::pluginManager('access')
      ->createInstance('none');
  }
  $access_plugin
    ->alterRouteDefinition($route);
  $collection
    ->add("view.{$view_id}.{$display_id}", $route);
}