template_preprocess_views_view_summary_unformatted

template_preprocess_views_view_summary_unformatted(&$variables)

Prepares variables for unformatted summary view templates.

Default template: views-view-summary-unformatted.html.twig.

Parameters

array $variables: An associative array containing:

  • view: A ViewExecutable object.
  • rows: The raw row data.
  • options: An array of options. Each option contains:
    • separator: A string to be placed between inline fields to keep them visually distinct.

File

core/modules/views/views.theme.inc, line 327
Preprocessors and helper functions to make theming easier.

Code

function template_preprocess_views_view_summary_unformatted(&$variables) {
  /** @var \Drupal\views\ViewExecutable $view */
  $view = $variables['view'];
  $argument = $view->argument[$view->build_info['summary_level']];

  $url_options = array();

  if (!empty($view->exposed_raw_input)) {
    $url_options['query'] = $view->exposed_raw_input;
  }

  $count = 0;
  $active_urls = array(
    // Force system path.
    \Drupal::url('<current>', [], ['alias' => TRUE]),
    // Could be an alias.
    \Drupal::url('<current>'),
  );
  $active_urls = array_combine($active_urls, $active_urls);

  // Collect all arguments for each row, to be able to alter them for example
  // by the validator. This is not done per single argument value, because
  // this could cause performance problems.
  $row_args = array();
  foreach ($variables['rows'] as $id => $row) {
    $row_args[$id] = $argument->summaryArgument($row);
  }
  $argument->processSummaryArguments($row_args);

  foreach ($variables['rows'] as $id => $row) {
    // Only false on first time.
    if ($count++) {
      $variables['rows'][$id]->separator = Xss::filterAdmin($variables['options']['separator']);
    }
    $variables['rows'][$id]->attributes = array();
    $variables['rows'][$id]->link = $argument->summaryName($row);
    $args = $view->args;
    $args[$argument->position] = $row_args[$id];

    if (!empty($argument->options['summary_options']['base_path'])) {
      $base_path = $argument->options['summary_options']['base_path'];
      $tokens = $view->getDisplay()->getArgumentsTokens();
      $base_path = $argument->globalTokenReplace($base_path, $tokens);
      // @todo Views should expect and store a leading /. See:
      //   https://www.drupal.org/node/2423913
      $url = Url::fromUserInput('/' . $base_path);
      try {
        /** @var \Symfony\Component\Routing\Route $route */
        $route = \Drupal::service('router.route_provider')->getRouteByName($url->getRouteName());
        $route_variables = $route->compile()->getVariables();
        $parameters = $url->getRouteParameters();

        foreach ($route_variables as $variable_name) {
          $parameters[$variable_name] = array_shift($args);
        }

        $url->setRouteParameters($parameters);
      }
      catch (Exception $e) {
        // If the given route doesn't exist, default to <front>
        $url = Url::fromRoute('<front>');
      }
    }
    else {
      $url = $view->getUrl($args)->setOptions($url_options);
    }
    $variables['rows'][$id]->url = $url->toString();
    $variables['rows'][$id]->count = intval($row->{$argument->count_alias});
    $variables['rows'][$id]->active = isset($active_urls[$variables['rows'][$id]->url]);
    $variables['rows'][$id]->attributes = new Attribute($variables['rows'][$id]->attributes);
  }
}
doc_Drupal
2016-10-29 09:46:53
Comments
Leave a Comment

Please login to continue.