You are here

function views_rss_core_preprocess_item_enclosure in Views RSS 8.2

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

Preprocess function for item <enclosure> element.

1 string reference to 'views_rss_core_preprocess_item_enclosure'
views_rss_core_views_rss_item_elements in modules/views_rss_core/views_rss_core.module
Implements hook_views_rss_item_elements().

File

modules/views_rss_core/views_rss_core.inc, line 264
Preprocess functions for Views RSS: Core Elements module.

Code

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;
  }
}