You are here

views_rss.module in Views RSS 8.3

Module providing fields-based views style plugin for RSS feed generation.

File

views_rss.module
View source
<?php

/**
 * @file
 * Module providing fields-based views style plugin for RSS feed generation.
 */

/**
 * Returns an array of item elements defined by other modules
 * with hook_views_rss_item_elements() and optionally altered with
 * hook_views_rss_item_elements_alter() implementations.
 */
function views_rss_get($key, $rebuild = FALSE) {
  static $data = array();
  if (!isset($data[$key]) || empty($data[$key]) || $rebuild === TRUE) {
    $cid = 'views_rss:' . $key;
    $cache = \Drupal::cache('data')
      ->get($cid);
    if (isset($cache->data) && $rebuild === FALSE) {
      $data[$key] = $cache->data;
    }
    else {

      // Fetch item elements provided by other modules. We need to manually call
      // each module so that we can know which module a given item came from.
      $data[$key] = array();
      $hook_name = 'views_rss_' . $key;
      $modules = \Drupal::moduleHandler()
        ->getImplementations($hook_name);
      foreach ($modules as $module) {
        $module_data = \Drupal::moduleHandler()
          ->invoke($module, $hook_name);
        if (isset($module_data) && is_array($module_data)) {
          $data[$key][$module] = $module_data;
        }
      }

      // Add namespaces not defined by any hook_views_rss_namespaces(),
      // but used in any of defined <channel> or <item> elements.
      // Let's also add "xmlns" prefix by default to such namespaces.
      $function = '_views_rss_process_' . $key;
      if (function_exists($function)) {
        $data[$key] = $function($data[$key]);
      }

      // Allow other modules to alter obtained item elements.
      \Drupal::moduleHandler()
        ->alter($hook_name, $data[$key]);

      // Store it infinitely in cache (rebuild only on cache clear).
      \Drupal::cache('data')
        ->set($cid, $data[$key]);
    }
  }
  return $data[$key];
}

/**
 * Add namespaces not defined by any hook_views_rss_namespaces(),
 * but used in any of defined <channel> or <item> elements.
 * Let's also add "xmlns" prefix by default to such namespaces.
 */
function _views_rss_process_namespaces($namespaces) {
  foreach (views_rss_get('channel_elements') as $module => $elements) {
    foreach (array_keys($elements) as $element) {
      list($namespace, $element_name) = views_rss_extract_element_names($element);
      if ($namespace && !isset($namespaces[$module][$namespace])) {
        $namespaces[$module][$namespace] = array(
          'prefix' => 'xmlns',
          'uri' => NULL,
        );
      }
    }
  }
  foreach (views_rss_get('item_elements') as $module => $elements) {
    foreach (array_keys($elements) as $element) {
      list($namespace, $element_name) = views_rss_extract_element_names($element);
      if ($namespace && !isset($namespaces[$module][$namespace])) {
        $namespaces[$module][$namespace] = array(
          'prefix' => 'xmlns',
          'uri' => NULL,
        );
      }
    }
  }
  return $namespaces;
}

/**
 * Add table aliases for additional fields used for altering view query.
 */
function _views_rss_process_date_sources($date_sources) {
  foreach ($date_sources as $module => $module_date_sources) {
    foreach ($module_date_sources as $base_table => $elements) {
      foreach ($elements as $element_name => $definition) {
        if (!isset($definition['alias'])) {
          $date_sources[$module][$base_table][$element_name]['alias'] = $element_name;
        }
      }
    }
  }
  return $date_sources;
}

/**
 * Extracts and returns an array containing element namespace and name.
 */
function views_rss_extract_element_names($element, $core_namespace = '') {
  if (!strstr($element, ':')) {
    $element = $core_namespace . ':' . $element;
  }
  return explode(':', $element);
}

/**
 * Element preprocess callback: replaces relative paths with absolute URLs.
 *
 * Based on preg_match from rel_to_abs module by lourenzo,
 * with added patch from issue #1335734 by joelstein.
 *
 * @see http://drupal.org/project/rel_to_abs
 * @see http://drupal.org/node/1335734
 */
function views_rss_rewrite_relative_paths(&$variables) {

  // Rewriting relative paths should be enabled by default,
  // so rewrite relative paths even if option value is not set.
  if (!isset($variables['view']->style_plugin->options['feed_settings']['absolute_paths']) || !empty($variables['view']->style_plugin->options['feed_settings']['absolute_paths'])) {
    global $base_path;
    foreach ($variables['elements'] as $delta => $element) {
      if (isset($element['value'])) {

        // Value is an array, so this is a set of subelements.
        if (is_array($element['value'])) {
          views_rss_rewrite_relative_paths($variables['elements'][$delta]['value']);
        }
        else {
          $pattern = '/(src|href)=(\'|")[^\\/]' . preg_quote($base_path, '/') . '/';
          $replacement = '$1=$2' . url('<front>', array(
            'absolute' => TRUE,
          ));
          $variables['elements'][$delta]['value'] = preg_replace($pattern, $replacement, $element['value']);
        }
      }
    }
  }
}

/**
 * Forms associative array from linear array,
 * or returns original array if already associative.
 */
function views_rss_map_assoc($array) {
  if (!(array_keys($array) !== range(0, count($array) - 1))) {
    $array = array_combine($array, $array);
  }
  return $array;
}

Functions

Namesort descending Description
views_rss_extract_element_names Extracts and returns an array containing element namespace and name.
views_rss_get Returns an array of item elements defined by other modules with hook_views_rss_item_elements() and optionally altered with hook_views_rss_item_elements_alter() implementations.
views_rss_map_assoc Forms associative array from linear array, or returns original array if already associative.
views_rss_rewrite_relative_paths Element preprocess callback: replaces relative paths with absolute URLs.
_views_rss_process_date_sources Add table aliases for additional fields used for altering view query.
_views_rss_process_namespaces Add namespaces not defined by any hook_views_rss_namespaces(), but used in any of defined <channel> or <item> elements. Let's also add "xmlns" prefix by default to such namespaces.