You are here

views_rss_media.inc in Views RSS: Media (MRSS) Elements 8

Same filename and directory in other branches
  1. 7 views_rss_media.inc

Preprocess functions for Views RSS: Media Elements module.

File

views_rss_media.inc
View source
<?php

/**
 * @file
 * Preprocess functions for Views RSS: Media Elements module.
 */
use Drupal\file\FileInterface;
use Drupal\image\Entity\ImageStyle;

/**
 * Preprocess function for item <media:content> element.
 */
function views_rss_media_preprocess_item_content(&$variables) {

  // No raw value = no 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' => 'media:content',
      'attributes' => array(),
    );

    // File entity found.
    if ($file instanceof FileInterface) {
      $mime_type = $file
        ->getMimeType();

      // Image style is defined, need to link to resized version.
      if ($image_style_name = $item['rendered']['#image_style']) {
        $image_style = ImageStyle::load($image_style_name);
        $uri = $image_style
          ->buildUri($file
          ->getFileUri());
        $element['attributes'] = array(
          'url' => file_create_url($uri),
          'fileSize' => filesize(\Drupal::service('file_system')
            ->realpath($uri)),
          'type' => $mime_type,
        );
      }
      else {
        $element['attributes'] = array(
          'url' => file_create_url($file
            ->getFileUri()),
          'fileSize' => $file
            ->getSize(),
          'type' => $mime_type,
        );
      }
      list($medium) = explode('/', $mime_type);
      $element['attributes']['medium'] = $medium;
    }
    elseif (!empty($item['rendered']['#markup'])) {
      $element['attributes']['url'] = $item['rendered']['#markup'];
    }
    $variables['elements'][] = $element;
  }
}

/**
 * Preprocess function for item <media:title> and <media:description> elements.
 */
function views_rss_media_preprocess_item_text(&$variables) {
  foreach ($variables['elements'] as $delta => $element) {
    if (!empty($element['value'])) {
      $type = 'plain';
      $value_decoded = htmlspecialchars_decode($element['value'], ENT_QUOTES);
      if ($value_decoded != strip_tags($value_decoded)) {
        $type = 'html';
      }
      $variables['elements'][$delta]['attributes']['type'] = $type;
    }
  }
}

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

  // No raw values = no 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' => 'media:thumbnail',
      'attributes' => array(),
    );

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

      // Image style is defined, need to link to resized version.
      if ($image_style_name = $item['rendered']['#image_style']) {
        $image_style = ImageStyle::load($image_style_name);
        $element['attributes']['url'] = file_create_url($image_style
          ->buildUri($file
          ->getFileUri()));
      }
      else {
        $element['attributes']['url'] = $file
          ->url();
      }
    }
    elseif (!empty($item['rendered']['#markup'])) {
      $element['attributes']['url'] = $item['rendered']['#markup'];
    }
    $variables['elements'][] = $element;
  }
}

/**
 * Preprocess function for item <media:category> element.
 *
 * @see views_rss_core_preprocess_item_category()
 */
function views_rss_media_preprocess_item_category(&$variables) {

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

    // 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::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->loadAllParents($item['raw']->entity
      ->id())) {
      foreach ($parents as $parent) {
        $values[] = $parent
          ->label();
      }
    }
    $element = array(
      'key' => 'media:category',
      'value' => implode('/', array_reverse($values)),
      'attributes' => array(
        'label' => $item['raw']->entity
          ->label(),
      ),
    );

    // Drupal uses term URL for domain attribute on main category element
    // (similar to media:category). RSS Best Practices say that 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?
    if ($item['raw']->target_id) {
      $element['attributes']['scheme'] = $item['raw']->entity
        ->url('canonical', array(
        'absolute' => TRUE,
      ));
    }
    $variables['elements'][] = $element;
  }
}

Functions

Namesort descending Description
views_rss_media_preprocess_item_category Preprocess function for item <media:category> element.
views_rss_media_preprocess_item_content Preprocess function for item <media:content> element.
views_rss_media_preprocess_item_text Preprocess function for item <media:title> and <media:description> elements.
views_rss_media_preprocess_item_thumbnail Preprocess function for item <enclosure> element.