You are here

function views_rss_core_preprocess_views_view_rss in Views RSS 8.2

Same name and namespace in other branches
  1. 8.3 modules/views_rss_core/views_rss_core.module \views_rss_core_preprocess_views_view_rss()

Prepares correct variables for RSS feed templates.

Drupal core template_preprocess_views_view_rss() forces values of some elements, thus making them not configurable through Views RSS (these are channel 'title', 'description' and 'link' elements). Because we want them all to be configurable through Views RSS' preprocess hooks, they need to be reprocessed here again, after template_preprocess_views_view_rss() has done its mess.

See also

template_preprocess_views_view_rss()

File

modules/views_rss_core/views_rss_core.module, line 294
Provides core <channel> and <item> elements for Views RSS module.

Code

function views_rss_core_preprocess_views_view_rss(&$variables) {
  $view = $variables['view'];
  $style = $view->style_plugin;
  if (count($view->result) > 0) {
    $max_pub_date = NULL;
    foreach ($variables['rows'] as $row) {
      foreach ($row['#row']->elements as $element) {
        if ($element['key'] === 'pubDate') {
          $this_pub_date = (string) $element['value'];
          if (strtotime($this_pub_date) > strtotime($max_pub_date)) {
            $max_pub_date = $this_pub_date;
          }
          break;
        }
      }
    }
    if ($max_pub_date !== NULL) {
      $variables['channel_elements']['pubDate'] = [
        '#type' => 'html_tag',
        '#tag' => 'pubDate',
        '#value' => $max_pub_date,
      ];
    }
  }

  // Get Views RSS-configured values for channel 'title', 'description'
  // and 'link' values and re-add them to theme variables, at the same time
  // removing them from $style->channel_elements, which would need to be
  // converted to XML again without them.
  foreach ($style->channel_elements as $delta => $channel_element) {
    if (is_array($channel_element) && isset($channel_element['key'])) {
      $key = $channel_element['key'];

      // These 3 elements have the same name as their $variables key.
      if (in_array($key, array(
        'title',
        'description',
        'link',
      ))) {

        // Assign the value received from Views RSS preprocess function, instead
        // the one added by template_preprocess_views_view_rss().
        $variables[$key] = $channel_element['value'];

        // Also, unset these elements from $style->channel_elements to avoid
        // having them printed out twice in feed's channel element.
        unset($style->channel_elements[$delta]);
      }

      // And then, why would all $variables key names use the same names as RSS
      // elements, right? Let's make one different! Thanks Drupal. Seriously.
      if ($key == 'language') {
        $variables['langcode'] = $channel_element['value'];
        unset($style->channel_elements[$delta]);
      }
    }
  }
}