ViewsLocalTask.php 4.73 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\views\Plugin\Derivative\ViewsLocalTask.
 */

namespace Drupal\views\Plugin\Derivative;

10
use Drupal\Core\State\StateInterface;
11 12
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
13 14 15 16 17 18 19
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides local task definitions for all views configured as local tasks.
 */
20
class ViewsLocalTask extends DeriverBase implements ContainerDeriverInterface {
21 22 23 24 25 26 27 28 29 30 31

  /**
   * The route provider.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface
   */
  protected $routeProvider;

  /**
   * The state key value store.
   *
32
   * @var \Drupal\Core\State\StateInterface
33 34 35 36 37 38 39 40
   */
  protected $state;

  /**
   * Constructs a \Drupal\views\Plugin\Derivative\ViewsLocalTask instance.
   *
   * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
   *   The route provider.
41
   * @param \Drupal\Core\State\StateInterface $state
42 43
   *   The state key value store.
   */
44
  public function __construct(RouteProviderInterface $route_provider, StateInterface $state) {
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    $this->routeProvider = $route_provider;
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static(
      $container->get('router.route_provider'),
      $container->get('state')
    );
  }

  /**
   * {@inheritdoc}
   */
62
  public function getDerivativeDefinitions($base_plugin_definition) {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    $this->derivatives = array();

    $view_route_names = $this->state->get('views.view_route_names');
    foreach ($this->getApplicableMenuViews() as $pair) {
      /** @var $executable \Drupal\views\ViewExecutable */
      list($executable, $display_id) = $pair;

      $executable->setDisplay($display_id);
      $menu = $executable->display_handler->getOption('menu');
      if (in_array($menu['type'], array('tab', 'default tab'))) {
        $plugin_id = 'view.' . $executable->storage->id() . '.' . $display_id;
        $route_name = $view_route_names[$executable->storage->id() . '.' . $display_id];

        // Don't add a local task for views which override existing routes.
        // @todo Alternative it could just change the existing entry.
        if ($route_name != $plugin_id) {
          continue;
        }

        $this->derivatives[$plugin_id] = array(
          'route_name' => $route_name,
          'weight' => $menu['weight'],
          'title' => $menu['title'],
        ) + $base_plugin_definition;

88
        // Default local tasks have themselves as root tab.
89
        if ($menu['type'] == 'default tab') {
90
          $this->derivatives[$plugin_id]['base_route'] = $route_name;
91 92 93 94 95 96 97
        }
      }
    }
    return $this->derivatives;
  }

  /**
98
   * Alters base_route and parent_id into the views local tasks.
99 100 101 102 103 104 105 106 107 108 109
   */
  public function alterLocalTasks(&$local_tasks) {
    $view_route_names = $this->state->get('views.view_route_names');

    foreach ($this->getApplicableMenuViews() as $pair) {
      /** @var $executable \Drupal\views\ViewExecutable */
      list($executable, $display_id) = $pair;

      $executable->setDisplay($display_id);
      $menu = $executable->display_handler->getOption('menu');

110
      // We already have set the base_route for default tabs.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
      if (in_array($menu['type'], array('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) {
131
            $local_tasks['views_view:' . $plugin_id]['base_route'] = $name;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
            // Skip after the first found route.
            break;
          }
        }
      }
    }
  }

  /**
   * Return a list of all views and display IDs that have a menu entry.
   *
   * @return array
   *   A list of arrays containing the $view and $display_id.
   * @code
   * array(
   *   array($view, $display_id),
   *   array($view, $display_id),
   * );
   * @endcode
   */
  protected function getApplicableMenuViews() {
153
    return Views::getApplicableViews('uses_menu_links');
154 155 156
  }

}