You are here

protected function PathPluginBase::getRoute in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/display/PathPluginBase.php \Drupal\views\Plugin\views\display\PathPluginBase::getRoute()
  2. 9 core/modules/views/src/Plugin/views/display/PathPluginBase.php \Drupal\views\Plugin\views\display\PathPluginBase::getRoute()

Generates a route entry for a given view and display.

Parameters

string $view_id: The ID of the view.

string $display_id: The current display ID.

Return value

\Symfony\Component\Routing\Route The route for the view.

3 calls to PathPluginBase::getRoute()
Page::getRoute in core/modules/views/src/Plugin/views/display/Page.php
Generates a route entry for a given view and display.
PathPluginBase::alterRoutes in core/modules/views/src/Plugin/views/display/PathPluginBase.php
Alters a collection of routes and replaces definitions to the view.
PathPluginBase::collectRoutes in core/modules/views/src/Plugin/views/display/PathPluginBase.php
Adds the route entry of a view to the collection.
1 method overrides PathPluginBase::getRoute()
Page::getRoute in core/modules/views/src/Plugin/views/display/Page.php
Generates a route entry for a given view and display.

File

core/modules/views/src/Plugin/views/display/PathPluginBase.php, line 126

Class

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

Namespace

Drupal\views\Plugin\views\display

Code

protected function getRoute($view_id, $display_id) {
  $defaults = [
    '_controller' => 'Drupal\\views\\Routing\\ViewPageController::handle',
    '_title_callback' => 'Drupal\\views\\Routing\\ViewPageController::getTitle',
    'view_id' => $view_id,
    'display_id' => $display_id,
    '_view_display_show_admin_links' => $this
      ->getOption('show_admin_links'),
  ];

  // @todo How do we apply argument validation?
  $path = $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;
  $argument_ids = array_keys((array) $this
    ->getOption('arguments'));
  $total_arguments = count($argument_ids);
  $argument_map = [];
  $bits = [];
  if (is_string($path)) {
    $bits = explode('/', $path);

    // 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_' . $arg_counter++;
        $bits[$pos] = '{' . $arg_id . '}';
        $argument_map[$arg_id] = $arg_id;
      }
      elseif (strpos($bit, '%') === 0) {

        // Use the name defined in the path.
        $parameter_name = substr($bit, 1);
        $arg_id = 'arg_' . $arg_counter++;
        $argument_map[$arg_id] = $parameter_name;
        $bits[$pos] = '{' . $parameter_name . '}';
      }
    }
  }

  // Add missing arguments not defined in the path, but added as handler.
  while ($total_arguments - $arg_counter > 0) {
    $arg_id = 'arg_' . $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;
    $argument_map[$arg_id] = $arg_id;
    $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 (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);

  // Set the argument map, in order to support named parameters.
  $route
    ->setOption('_view_argument_map', $argument_map);
  $route
    ->setOption('_view_display_plugin_id', $this
    ->getPluginId());
  $route
    ->setOption('_view_display_plugin_class', static::class);
  $route
    ->setOption('_view_display_show_admin_links', $this
    ->getOption('show_admin_links'));

  // Store whether the view will return a response.
  $route
    ->setOption('returns_response', !empty($this
    ->getPluginDefinition()['returns_response']));

  // Symfony 4 requires that UTF-8 route patterns have the "utf8" option set
  $route
    ->setOption('utf8', TRUE);
  return $route;
}