You are here

public function ViewsLocalTask::getDerivativeDefinitions in Webform Views Integration 8.5

Gets the definition of all derivatives of a base plugin.

Parameters

array $base_plugin_definition: The definition array of the base plugin.

Return value

array An array of full derivative definitions keyed on derivative id.

Overrides DeriverBase::getDerivativeDefinitions

See also

getDerivativeDefinition()

File

src/Plugin/Derivative/ViewsLocalTask.php, line 89

Class

ViewsLocalTask
Provides local task definitions webform submission views.

Namespace

Drupal\webform_views\Plugin\Derivative

Code

public function getDerivativeDefinitions($base_plugin_definition) {
  static $recursion = 0;
  $this->derivatives = [];
  $view_route_names = $this->state
    ->get('views.view_route_names');
  foreach (webform_views_applicable_views() as $trio) {
    list($view_id, $display_id, $path) = $trio;

    /** @var $executable \Drupal\views\ViewExecutable */
    $executable = $this->viewStorage
      ->load($view_id)
      ->getExecutable();
    $executable
      ->setDisplay($display_id);
    $menu = $executable->display_handler
      ->getOption('menu');
    $plugin_id = 'view.' . $executable->storage
      ->id() . '.' . $display_id;
    $route_name = $view_route_names[$executable->storage
      ->id() . '.' . $display_id];
    $parent_path = explode('/', $path);
    array_pop($parent_path);
    $parent_path = implode('/', $parent_path);
    $pattern = '/' . $parent_path;
    if ($routes = $this->routeProvider
      ->getRoutesByPattern($pattern)) {
      foreach ($routes
        ->all() as $name => $route) {

        // Array reverse is here because we prefer the lower level tasks over
        // higher level ones.
        foreach (array_reverse($this->localTaskManager
          ->getLocalTasksForRoute($name)) as $leveled_local_tasks) {
          foreach ($leveled_local_tasks as $local_task) {

            /** @var \Drupal\Core\Menu\LocalTaskInterface $local_task */
            if ($local_task
              ->getRouteName() == $name) {
              $definition = [
                'route_name' => $route_name,
                'weight' => $menu['weight'],
                'title' => $menu['title'],
              ] + $base_plugin_definition;
              if ($local_task
                ->getPluginDefinition()['parent_id']) {
                $definition['parent_id'] = $local_task
                  ->getPluginDefinition()['parent_id'];
              }
              else {
                $definition['base_route'] = $local_task
                  ->getPluginDefinition()['base_route'];
              }
              $this->derivatives['webform_views:' . $plugin_id] = $definition;

              // Skip after the first found route.
              break 3;
            }
          }
        }
      }
      $recursion++;
      if ($recursion == 1 && isset($name) && !isset($this->derivatives['webform_views:' . $plugin_id])) {

        // As a last resort try to look up a local task whose ID equals the
        // route name because most of local tasks copy-paste their IDs after
        // the route they represent.
        $parent_task = $this->localTaskManager
          ->getDefinition($name, FALSE);
        if ($parent_task) {
          $this->derivatives['webform_views:' . $plugin_id] = [
            'route_name' => $route_name,
            'weight' => $menu['weight'],
            'title' => $menu['title'],
            'parent_id' => $parent_task['parent_id'] ?: $parent_task['id'],
          ] + $base_plugin_definition;
        }
        else {
          $this->derivatives['webform_views:' . $plugin_id] = [
            'route_name' => $route_name,
            'weight' => $menu['weight'],
            'title' => $menu['title'],
            'base_route' => $name,
          ] + $base_plugin_definition;
        }
      }
    }
  }
  return $this->derivatives;
}