You are here

views_rss_core.inc in Views RSS 8.2

Preprocess functions for Views RSS: Core Elements module.

File

modules/views_rss_core/views_rss_core.inc
View source
<?php

/**
 * @file
 * Preprocess functions for Views RSS: Core Elements module.
 */
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\file\FileInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\core\Url;

/**
 * Preprocess function for channel <title> element.
 */
function views_rss_core_preprocess_channel_title(&$variables) {
  $config = \Drupal::config('system.site');
  if ($variables['view']->display_handler
    ->getOption('sitename_title')) {
    $title = $config
      ->get('name');
    if ($slogan = $config
      ->get('slogan')) {
      $title .= ' - ' . $slogan;
    }
  }
  else {
    $title = $variables['view']
      ->getTitle();
  }
  $variables['elements'][0]['value'] = strip_tags($title);
}

/**
 * Preprocess function for channel <description> element.
 */
function views_rss_core_preprocess_channel_description(&$variables) {
  if (empty($variables['elements'][0]['value'])) {
    $variables['elements'][0]['value'] = \Drupal::config('system.site')
      ->get('slogan');
  }
}

/**
 * Preprocess function for channel <link> element.
 */
function views_rss_core_preprocess_channel_link(&$variables) {

  // The link element identifies the URL of the web site associated with
  // the feed (and not the feed's URL, as Drupal makes you think).
  // See http://www.rssboard.org/rss-profile#element-channel-link
  $variables['elements'][0]['value'] = $GLOBALS['base_url'] . '/';
}

/**
 * Preprocess function for channel <atom:link> element.
 */
function views_rss_core_preprocess_channel_atom_link(&$variables) {

  // Use the request path not the view path here due to possible exposed filters
  // changing the effective path for views.
  $url = Url::createFromRequest(\Drupal::request())
    ->setOption('absolute', TRUE)
    ->setOption('query', \Drupal::request()->query
    ->all())
    ->toString();
  $variables['elements'][0]['attributes'] = [
    'rel' => 'self',
    'href' => $url,
  ];
}

/**
 * Preprocess function for channel <language> element.
 */
function views_rss_core_preprocess_channel_language(&$variables) {
  if (empty($variables['elements'][0]['value'])) {
    $variables['elements'][0]['value'] = Html::escape(\Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId());
  }
}

/**
 * Preprocess function for channel <category> element.
 */
function views_rss_core_preprocess_channel_category(&$variables) {

  // No value = no preprocessing.
  if (empty($variables['elements'][0]['value'])) {
    return;
  }
  $elements = array();
  $categories = explode(',', $variables['elements'][0]['value']);
  foreach ($categories as $category) {
    $elements[] = array(
      'key' => 'category',
      'value' => trim($category),
    );
  }
  $variables['elements'] = $elements;
}

/**
 * Preprocess function for channel <image> element.
 */
function views_rss_core_preprocess_channel_image(&$variables) {

  // No value = no preprocessing.
  if (empty($variables['elements'][0]['value'])) {
    return;
  }
  $image_path = $variables['elements'][0]['value'];

  // Get value of channel <title> element from its preprocess function.
  views_rss_core_preprocess_channel_title($variables);
  $title = $variables['elements'][0]['value'];

  // Create subelements array.
  $variables['elements'][0]['value'] = array(
    // The image's url element identifies the URL of the image.
    'url' => file_create_url($image_path),
    // The image's title element SHOULD have the same text as the channel's
    // title element and be suitable for use as the alt attribute of the img
    // tag in an HTML rendering.
    'title' => $title,
    // The image's link element identifies the URL of the web site represented
    // by the image (not the feed URL).
    'link' => $GLOBALS['base_url'] . '/',
  );

  // Try to get image description from website's mission.
  if ($site_slogan = \Drupal::config('system.site')
    ->get('slogan')) {
    $variables['elements'][0]['value']['description'] = $site_slogan;
  }

  // Get image width and height.
  $image = Drupal::service('image.factory')
    ->get($image_path);
  if (!empty($image)) {
    $variables['elements'][0]['value']['width'] = $image
      ->getWidth();
    $variables['elements'][0]['value']['height'] = $image
      ->getHeight();
  }
}

/**
 * Preprocess function for channel <lastBuildDate> element.
 *
 * It will return values for date element providing that original Views query
 * was modified appropriately by views_rss_core_views_query_alter() by adding
 * a field to SELECT clause retrieving object modification timestamp (for
 * <lastBuildDate>).
 */
function views_rss_core_preprocess_channel_date(&$variables) {

  // @TODO make this function friendly for already formatted dates.
  if (count($variables['view']->result) > 0) {
    $max_date = 0;
    foreach ($variables['view']->result as $row) {
      $key = $variables['elements'][0]['key'];
      if (isset($row->{$key}) && $row->{$key} > $max_date) {
        $max_date = $row->{$key};
      }
    }
    if ($max_date) {
      $variables['elements'][0]['value'] = date('r', $max_date);
    }
  }
}

/**
 * Preprocess function for channel <lastBuildDate> element.
 */
function views_rss_core_preprocess_channel_last_build_date(&$variables) {
  $variables['elements'][0]['value'] = date('r');
}

/**
 * Preprocess function for channel <skipHours> and <skipDays> elements.
 */
function views_rss_core_preprocess_channel_skip(&$variables) {

  // No value = no preprocessing.
  if (empty($variables['elements'][0]['value'])) {
    return;
  }
  $elements = array();
  $skips = strip_tags($variables['elements'][0]['value']);
  if (!empty($skips)) {
    foreach (explode(',', $skips) as $skip_value) {
      $elements[] = array(
        'key' => $variables['elements'][0]['key'] == 'skipHours' ? 'hour' : 'day',
        'value' => trim($skip_value),
      );
    }
  }
  $variables['elements'][0]['value'] = $elements;
}

/**
 * Preprocess function for channel <cloud> element.
 */
function views_rss_core_preprocess_channel_cloud(&$variables) {

  // No value = no preprocessing.
  if (empty($variables['elements'][0]['value'])) {
    return;
  }
  if ($url = parse_url($variables['elements'][0]['value'])) {
    $variables['elements'][0]['value'] = NULL;
    $variables['elements'][0]['attributes'] = array(
      'domain' => $url['host'],
      'port' => $url['port'],
      'path' => $url['path'],
      'registerProcedure' => $url['fragment'],
      'protocol' => $url['scheme'],
    );
  }
}

/**
 * Preprocess function for item <category> element.
 *
 * See http://www.rssboard.org/rss-profile#element-channel-item-category
 * for RSS Advisory Board requirements/recommendations.
 *
 * @see RSSCategoryFormatter::viewElements()
 */
function views_rss_core_preprocess_item_category(&$variables) {

  // No raw values = no preprocessing.
  if (empty($variables['raw']['items'])) {
    return;
  }
  $variables['elements'] = array();
  foreach ($variables['raw']['items'] as $item) {

    // When 'Content: <vocabulary_name>' was selected for source of category
    // element, we will get all we need in raw values.
    if (!empty($item['raw']) && is_object($item['raw']) && $item['raw']->target_id) {
      $term = $item['raw']->entity;
    }
    elseif (!empty($item['tid'])) {
      $term = Term::load($item['tid']);
    }

    // According to RSS Advisory Board, the category's value should be
    // a slash-delimited string that identifies a hierarchical position
    // in the taxonomy.
    $values = array();

    // Load parent term objects (this includes original term as well).
    if ($parents = \Drupal::service('entity_type.manager')
      ->getStorage('taxonomy_term')
      ->loadAllParents($term
      ->id())) {
      foreach ($parents as $parent) {
        $values[] = $parent
          ->label();
      }
    }
    $element = array(
      'key' => 'category',
      'value' => implode('/', array_reverse($values)),
      // Drupal uses term URL for domain attribute. RSS Best Practices say that
      // a domain attribute identifies the category's taxonomy - which suggests
      // either vocabulary name or its URL. We don't have any safe way to know
      // public vocabulary URL, could use its name instead though? @TODO?
      'attributes' => array(
        'domain' => $term
          ->url('canonical', array(
          'absolute' => TRUE,
        )),
      ),
    );
    $variables['elements'][] = $element;
  }
}

/**
 * Preprocess function for item <enclosure> element.
 */
function views_rss_core_preprocess_item_enclosure(&$variables) {

  // No raw values = no file preprocessing.
  if (empty($variables['raw']['items'])) {
    return;
  }
  $variables['elements'] = array();
  foreach ($variables['raw']['items'] as $item) {

    // File fields.
    if (!empty($item['rendered']['#file'])) {
      $file = $item['rendered']['#file'];
    }

    // Image fields.
    if (!empty($item['rendered']['#item']->entity)) {
      $file = $item['rendered']['#item']->entity;
    }

    // Start building RSS element.
    $element = array(
      'key' => 'enclosure',
      'attributes' => array(),
    );

    // Handle the situation where we are dealing with an embedded YouTube video.
    $class = '\\Drupal\\video_embed_field\\Plugin\\Field\\FieldType\\VideoEmbedField';
    if ($item['raw'] instanceof $class) {
      $item['rendered']['#markup'] = $item['raw']->value;
      $element['attributes']['type'] = 'application/x-shockwave-flash';
      $element['attributes']['length'] = 'unknown';
    }

    // File entity found.
    if (!empty($file) && $file instanceof FileInterface) {

      // Image style is defined, need to link to resized version.
      if (!empty($item['rendered']['#image_style'])) {
        $image_style_name = $item['rendered']['#image_style'];
        $image_uri = $file
          ->getFileUri();
        $image_style = \Drupal::service('entity_type.manager')
          ->getStorage('image_style')
          ->load($image_style_name);
        $image_style_uri = $image_style
          ->buildUri($image_uri);

        // If the derivative doesn't exist yet, we won't be able to get its size
        // to add it to the 'length' attribute, so we need to create it first.
        if (!file_exists($image_style_uri)) {
          $image_style
            ->createDerivative($image_uri, $image_style_uri);
        }
        $element['attributes'] = array(
          'url' => $image_style
            ->buildUrl($image_uri),
          'length' => filesize($image_style_uri),
          'type' => $file
            ->getMimeType(),
        );
      }
      else {
        $element['attributes'] = array(
          'url' => $file
            ->createFileUrl(FALSE),
          'length' => $file
            ->getSize(),
          'type' => $file
            ->getMimeType(),
        );
      }
    }
    elseif (!empty($item['rendered']['#markup'])) {

      // Hack for using CDN files.
      // @todo make this more generalized to handle file and image entities.
      _convert_to_external_file($item['rendered']['#markup'], TRUE);
      $original_file = $item['rendered']['#markup'];
      $element['attributes']['url'] = $item['rendered']['#markup'];

      // Load the file length and type.
      $headers = get_headers($original_file, $format = 1);
      if (empty($element['attributes']['length']) && !empty($headers['Content-Length'])) {
        $element['attributes']['length'] = $headers['Content-Length'];
      }
      if (empty($element['attributes']['type']) && !empty($headers['Content-Type'])) {
        $element['attributes']['type'] = $headers['Content-Type'];
      }
    }
    $variables['elements'][] = $element;
  }
}

/**
 * Preprocess function for item <guid> element.
 */
function views_rss_core_preprocess_item_guid(&$variables) {

  // No value = no preprocessing.
  if (empty($variables['elements'][0]['value'])) {
    return;
  }

  // Hack for using CDN files.
  // @todo make this into a hook of some kind
  _convert_to_external_file($variables['elements'][0]['value']);
  $is_permalink = 'false';
  if (!empty($variables['item']['views_rss_core']['link']) && UrlHelper::isValid($variables['elements'][0]['value'], TRUE)) {
    $is_permalink = 'true';
  }
  $variables['elements'][0]['attributes']['isPermaLink'] = $is_permalink;
}

/**
 * Preprocess function for item <source> element.
 */
function views_rss_core_preprocess_item_source(&$variables) {
  $config = \Drupal::config('system.site');

  // Allow override of title based on view->args settings.
  // @todo find a better way of overriding title
  if (isset($variables['view']->args['title'])) {
    $title = $variables['view']->args['title'];
  }
  elseif ($variables['view']->display_handler
    ->getOption('sitename_title')) {
    $title = $config
      ->get('name');
    if ($slogan = $config
      ->get('slogan')) {
      $title .= ' - ' . $slogan;
    }
  }
  else {
    $title = $variables['view']
      ->getTitle();
  }
  $url_options = array(
    'absolute' => TRUE,
  );
  $input = $variables['view']
    ->getExposedInput();
  if ($input) {
    $url_options['query'] = $input;
  }

  // @todo handle all url situations including exposed input
  $variables['elements'][0]['value'] = strip_tags($title);
  $url = Url::createFromRequest(\Drupal::request())
    ->setOption('absolute', TRUE)
    ->setOption('query', \Drupal::request()->query
    ->all())
    ->toString();
  $variables['elements'][0]['attributes']['url'] = $url;
}

/**
 * Preprocess function for item <dc:creator> element.
 * This strips stray "Author" text leaving just Author 1, Author 2
 *
 * @todo make this more generalized, part of dc module, and a hook.
 */
function views_rss_core_preprocess_item_dc_creator(&$variables) {
  if (empty($variables['elements'][0]['value'])) {
    return;
  }
  $authors = $variables['elements'][0]['value'];
  $authors = str_replace('   Author', '', $authors);
  $author_array = explode(',', $authors);
  $elements = array();
  foreach ($author_array as $dc_created) {

    // Strip Role and everything after.
    $dc_created = preg_replace('/Role(.*)$/s', '', $dc_created);
    $elements[] = array(
      'key' => 'dc:creator',
      'value' => trim($dc_created),
    );
  }
  $variables['elements'] = $elements;
}

/**
 * Preprocess function for item <description> element.
 */
function views_rss_core_preprocess_item_description(&$variables) {

  //echo "DESCRIPTION " . $variables['elements'][0]['value'];
}

/**
 * Convert CDN internal name to external name.
 */
function _convert_to_external_file(&$filename, $force_http = TRUE) {

  // Strip /sites/ if it exists at the beginning of the filename.
  if (preg_match('/^\\/sites\\/\\//', $filename)) {
    $filename = preg_replace('/^\\/sites\\//', '', $filename);
  }

  // If the filename begins with / then add the url.
  if (preg_match('/^\\/[^\\/]/', $filename)) {
    $filename = $GLOBALS['base_url'] . $filename;
  }

  // If the filename begins with // then add the scheme.
  if (preg_match('/^\\/\\//', $filename)) {
    $scheme = \Drupal::request()
      ->getScheme();
    if ($force_http) {
      $scheme = 'http';
    }
    $filename = $scheme . ":" . $filename;
  }
}

/**
 * Preprocess function for item <link> element.
 */
function views_rss_core_preprocess_item_link(&$variables) {

  // Hack for using CDN files.
  // @todo make this more generalized to handle file and image entities.
  _convert_to_external_file($variables['elements'][0]['value']);
  return;
}

/**
 * Preprocess function for item <link> element.
 */
function views_rss_core_preprocess_item_author(&$variables) {

  // @TODO move this into hooks.
  if (empty($variables['elements'][0]['value'])) {
    return;
  }
  $authors = $variables['elements'][0]['value'];
  $authors = str_replace('Author', '', $authors);
  $author_array = explode(',', $authors);
  $elements = array();
  foreach ($author_array as $dc_created) {

    // Strip Role and everything after.
    $dc_created = preg_replace('/Role(.*)$/s', '', $dc_created);
    $elements[] = array(
      'key' => 'author',
      'value' => trim($dc_created),
    );

    // Just do one for now.
    break;
  }
  $variables['elements'] = $elements;
}

Functions

Namesort descending Description
views_rss_core_preprocess_channel_atom_link Preprocess function for channel <atom:link> element.
views_rss_core_preprocess_channel_category Preprocess function for channel <category> element.
views_rss_core_preprocess_channel_cloud Preprocess function for channel <cloud> element.
views_rss_core_preprocess_channel_date Preprocess function for channel <lastBuildDate> element.
views_rss_core_preprocess_channel_description Preprocess function for channel <description> element.
views_rss_core_preprocess_channel_image Preprocess function for channel <image> element.
views_rss_core_preprocess_channel_language Preprocess function for channel <language> element.
views_rss_core_preprocess_channel_last_build_date Preprocess function for channel <lastBuildDate> element.
views_rss_core_preprocess_channel_link Preprocess function for channel <link> element.
views_rss_core_preprocess_channel_skip Preprocess function for channel <skipHours> and <skipDays> elements.
views_rss_core_preprocess_channel_title Preprocess function for channel <title> element.
views_rss_core_preprocess_item_author Preprocess function for item <link> element.
views_rss_core_preprocess_item_category Preprocess function for item <category> element.
views_rss_core_preprocess_item_dc_creator Preprocess function for item <dc:creator> element. This strips stray "Author" text leaving just Author 1, Author 2
views_rss_core_preprocess_item_description Preprocess function for item <description> element.
views_rss_core_preprocess_item_enclosure Preprocess function for item <enclosure> element.
views_rss_core_preprocess_item_guid Preprocess function for item <guid> element.
views_rss_core_preprocess_item_link Preprocess function for item <link> element.
views_rss_core_preprocess_item_source Preprocess function for item <source> element.
_convert_to_external_file Convert CDN internal name to external name.