You are here

public function CoreViewsFacetSourceBase::getViewsArgumentsMap in Core Views Facets 8

Retrieves the current views arguments map and returns a detailed version.

Return value

array Detail enriched argument data.

2 calls to CoreViewsFacetSourceBase::getViewsArgumentsMap()
CoreViewsContextualFilter::fillFacetsWithResults in src/Plugin/facets/facet_source/CoreViewsContextualFilter.php
CoreViewsExposedFilter::fillFacetsWithResults in src/Plugin/facets/facet_source/CoreViewsExposedFilter.php

File

src/Plugin/facets/facet_source/CoreViewsFacetSourceBase.php, line 222

Class

CoreViewsFacetSourceBase
Provide common functions for core Views based facet sources.

Namespace

Drupal\core_views_facets\Plugin\facets\facet_source

Code

public function getViewsArgumentsMap() {
  if (empty($this->view
    ->getDisplay()
    ->getRoutedDisplay())) {
    return [];
  }
  $views_arguments = [];
  $arguments = $this->view
    ->getHandlers('argument', $this->view->current_display);
  foreach ($arguments as $argument_id => $argument) {
    $views_arguments[$argument_id] = $this->view->display_handler
      ->getHandler('argument', $argument_id);
  }
  reset($views_arguments);
  $route_name = $this->view
    ->getUrl()
    ->getRouteName();
  try {
    $route = $this->routeProvider
      ->getRouteByName($route_name);
  } catch (RouteNotFoundException $e) {
    return [];
  }
  $route_map = $route
    ->hasOption('_view_argument_map') ? $route
    ->getOption('_view_argument_map') : [];

  // Where do arguments come from?
  if ($this
    ->isRenderedInCurrentRequest()) {
    if ($ajax_arguments = drupal_static('core_views_contextual_filter_ajax_arguments')) {
      $argument_source = 'view_ajax';
      reset($ajax_arguments);
    }
    else {
      $argument_source = 'view_page';
    }
  }
  else {
    $argument_source = 'view_defaults';
  }

  // Each argument can only be active, if each previous argument was active
  // too. This variable is set to FALSE for all following arguments, when any
  // one argument is inactive.
  $active_value = TRUE;
  $map = [];

  // For all I can tell, the association view argument <-> route argument
  // happens entirely by counting up in both arrays...
  foreach ($route_map as $attribute => $parameter_name) {
    $current_map_entry = [];

    // Allow parameters be pulled from the request.
    // The map stores the actual name of the parameter in the request. Views
    // which override existing controller, use for example 'node' instead of
    // arg_nid as name.
    if (isset($map[$attribute])) {
      $attribute = $map[$attribute];
    }
    $current_map_entry['route_parameter'] = $attribute;
    $current_map_entry['views_argument_id'] = key($views_arguments);
    $current_map_entry['views_argument'] = current($views_arguments);
    next($views_arguments);
    $current_map_entry['neutral_value'] = empty($current_map_entry['views_argument']->options['exception']['value']) ? 'all' : $current_map_entry['views_argument']->options['exception']['value'];
    switch ($argument_source) {
      case 'view_page':

        // This is views page request, standard procedure.
        if ($current_request_argument = $this->routeMatch
          ->getRawParameter($attribute)) {
        }
        else {
          $current_request_argument = $this->routeMatch
            ->getParameter($attribute);
        }
        if (isset($current_request_argument)) {
          $current_map_entry['value'] = $current_request_argument;
          $current_map_entry['active'] = $active_value;
        }
        else {
          $current_map_entry['value'] = $current_map_entry['neutral_value'];
          $current_map_entry['active'] = FALSE;
          $active_value = FALSE;
        }
        break;
      case 'view_ajax':
        if (current($ajax_arguments)) {
          $current_map_entry['value'] = current($ajax_arguments);
          $current_map_entry['active'] = $active_value;
          if ($current_map_entry['value'] == $current_map_entry['neutral_value']) {
            $current_map_entry['active'] = FALSE;
          }
          next($ajax_arguments);
        }
        else {
          $current_map_entry['value'] = $current_map_entry['neutral_value'];
          $current_map_entry['active'] = FALSE;
          $active_value = FALSE;
        }
        break;
      default:

        // Just add the defaults.
        $current_map_entry['value'] = $current_map_entry['neutral_value'];
        $current_map_entry['active'] = FALSE;
        $active_value = FALSE;
    }
    $map[$current_map_entry['views_argument_id']] = $current_map_entry;
  }
  return $map;
}