Adds the route entry of a view to the collection.
\Symfony\Component\Routing\RouteCollection $collection: A collection of routes that should be registered for this resource.
Overrides DisplayRouterInterface::collectRoutes
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);
}