You are here

public function ViewsLocalTask::alterLocalTasks in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/Derivative/ViewsLocalTask.php \Drupal\views\Plugin\Derivative\ViewsLocalTask::alterLocalTasks()

Alters base_route and parent_id into the views local tasks.

File

core/modules/views/src/Plugin/Derivative/ViewsLocalTask.php, line 108

Class

ViewsLocalTask
Provides local task definitions for all views configured as local tasks.

Namespace

Drupal\views\Plugin\Derivative

Code

public function alterLocalTasks(&$local_tasks) {
  $view_route_names = $this->state
    ->get('views.view_route_names');
  foreach ($this
    ->getApplicableMenuViews() as $pair) {
    list($view_id, $display_id) = $pair;

    /** @var \Drupal\views\ViewExecutable $executable */
    $executable = $this->viewStorage
      ->load($view_id)
      ->getExecutable();
    $executable
      ->setDisplay($display_id);
    $menu = $executable->display_handler
      ->getOption('menu');

    // We already have set the base_route for default tabs.
    if (in_array($menu['type'], [
      'tab',
    ])) {
      $plugin_id = 'view.' . $executable->storage
        ->id() . '.' . $display_id;
      $view_route_name = $view_route_names[$executable->storage
        ->id() . '.' . $display_id];

      // Don't add a local task for views which override existing routes.
      if ($view_route_name != $plugin_id) {
        unset($local_tasks[$plugin_id]);
        continue;
      }

      // Find out the parent route.
      // @todo Find out how to find both the root and parent tab.
      $path = $executable->display_handler
        ->getPath();
      $split = explode('/', $path);
      array_pop($split);
      $path = implode('/', $split);
      $pattern = '/' . str_replace('%', '{}', $path);
      if ($routes = $this->routeProvider
        ->getRoutesByPattern($pattern)) {
        foreach ($routes
          ->all() as $name => $route) {
          $local_tasks['views_view:' . $plugin_id]['base_route'] = $name;

          // Skip after the first found route.
          break;
        }
      }
    }
  }
}