You are here

function template_preprocess_views_view_rss in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/views/views.theme.inc \template_preprocess_views_view_rss()

Prepares variables for RSS feed templates.

Default template: views-view-rss.html.twig.

Parameters

array $variables: An associative array containing:

  • view: A ViewExecutable object.
  • rows: The raw row data.

File

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

Code

function template_preprocess_views_view_rss(&$variables) {
  $view = $variables['view'];
  $items = $variables['rows'];
  $style = $view->style_plugin;
  $config = \Drupal::config('system.site');

  // The RSS 2.0 "spec" doesn't indicate HTML can be used in the description.
  // We strip all HTML tags, but need to prevent double encoding from properly
  // escaped source data (such as &amp becoming &).
  $variables['description'] = Html::decodeEntities(strip_tags($style
    ->getDescription()));
  if ($view->display_handler
    ->getOption('sitename_title')) {
    $title = $config
      ->get('name');
    if ($slogan = $config
      ->get('slogan')) {
      $title .= ' - ' . $slogan;
    }
  }
  else {
    $title = $view
      ->getTitle();
  }
  $variables['title'] = $title;

  // Figure out which display which has a path we're using for this feed. If
  // there isn't one, use the global $base_url
  $link_display_id = $view->display_handler
    ->getLinkDisplay();
  if ($link_display_id && ($display = $view->displayHandlers
    ->get($link_display_id))) {
    $url = $view
      ->getUrl(NULL, $link_display_id);
  }

  /** @var \Drupal\Core\Url $url */
  if ($url) {
    $url_options = array(
      'absolute' => TRUE,
    );
    if (!empty($view->exposed_raw_input)) {
      $url_options['query'] = $view->exposed_raw_input;
    }

    // Compare the link to the default home page; if it's the default home page,
    // just use $base_url.
    $url_string = $url
      ->setOptions($url_options)
      ->toString();
    if ($url_string === Url::fromUserInput($config
      ->get('page.front'))
      ->toString()) {
      $url_string = Url::fromRoute('<front>')
        ->setAbsolute()
        ->toString();
    }
    $variables['link'] = $url_string;
  }
  $variables['langcode'] = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $variables['namespaces'] = new Attribute($style->namespaces);
  $variables['items'] = $items;
  $variables['channel_elements'] = \Drupal::service('renderer')
    ->render($style->channel_elements);

  // During live preview we don't want to output the header since the contents
  // of the feed are being displayed inside a normal HTML page.
  if (empty($variables['view']->live_preview)) {
    $variables['view']
      ->getResponse()->headers
      ->set('Content-Type', 'application/rss+xml; charset=utf-8');
  }
}