You are here

views_rss_core.field.inc in Views RSS 6.2

Same filename and directory in other branches
  1. 7.2 modules/views_rss_core/views_rss_core.field.inc

Field formatters for Views RSS: Core Elements module.

File

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

/**
 * @file
 * Field formatters for Views RSS: Core Elements module.
 */

/**
 * Implementation of hook_field_formatter_info().
 */
function views_rss_core_field_formatter_info() {
  $formatters = array(
    // Generic image formatter for <enclosure> element.
    'enclosure_image' => array(
      'label' => t('RSS <enclosure> element: image: original size'),
      'field types' => array(
        'image',
        'filefield',
      ),
    ),
  );

  // Additional formatters for imagecache module.
  if (module_exists('imagecache')) {
    foreach (imagecache_presets() as $preset) {

      // Imagecache image formatter for <enclosure> element.
      $formatters['enclosure_image' . $preset['presetname']] = array(
        'label' => t('RSS <enclosure> element: image: @preset', array(
          '@preset' => $preset['presetname'],
        )),
        'field types' => array(
          'image',
          'filefield',
        ),
      );
    }
  }

  // Generic file formatter for <enclosure> element.
  $formatters['enclosure_file'] = array(
    'label' => t('RSS <enclosure> element: file'),
    'field types' => array(
      'filefield',
      'text',
      'link',
    ),
  );
  return $formatters;
}

/**
 * No, this is not an implementation of hook_field_formatter_view().
 * This hook does not exist in D6. This function only more or less simulates it,
 * but in reality it is just a standard theme callback.
 */
function views_rss_core_field_formatter_view($variables) {

  // Inside a view $element may contain NULL data. In that case, just return.
  if (empty($variables['#item']['fid']) && empty($variables['#item']['url']) && empty($variables['#item']['safe'])) {
    return;
  }

  // Is this an image formatter?
  $uri = NULL;

  // #formatter could have values like "enclosure_image<preset_name>".
  if (strpos($variables['#formatter'], 'enclosure_image') !== FALSE) {

    // Get full image URL based on provided imagecache preset.
    $preset_name = str_replace('enclosure_image', '', $variables['#formatter']);
    if (!empty($preset_name) && ($preset = imagecache_preset_by_name($preset_name))) {
      $uri = imagecache_create_url($preset_name, $variables['#item']['filepath']);

      // Get file size of image preset file (if it has already been created,
      // otherwise just create it first and then get file size).
      $path = imagecache_create_path($preset_name, $variables['#item']['filepath']);
      if (file_exists($path) || imagecache_build_derivative($preset['actions'], $variables['#item']['filepath'], $path)) {
        $variables['#item']['filesize'] = filesize($path);
      }
    }
  }
  if (empty($uri)) {
    if (!empty($variables['#item']['filepath'])) {
      $uri = url($variables['#item']['filepath'], array(
        'absolute' => TRUE,
      ));
    }
    elseif (!empty($variables['#item']['url']) || !empty($variables['#item']['safe'])) {
      $uri = !empty($variables['#item']['url']) ? $variables['#item']['url'] : $variables['#item']['safe'];

      // Great idea to use get_headers() here by Dan Feidt,
      // http://drupal.org/user/60005 Thanks Dan!
      $headers = get_headers($uri, $format = 1);
      $variables['#item']['filesize'] = $headers['Content-Length'];
      $variables['#item']['filemime'] = $headers['Content-Type'];
    }
  }

  // XML element array in format_xml_elements() format.
  $rss_element = array(
    'key' => 'enclosure',
    'attributes' => array(
      'url' => $uri,
      'length' => $variables['#item']['filesize'],
      'type' => $variables['#item']['filemime'],
    ),
  );

  // Views do not pass #type variable to this theme function,
  // so we should be safe to assume that if this variable exists,
  // we are displaying the field on a normal page, and in such case
  // return only XML markup instead of extended element array.
  if (isset($variables['#type']) && $variables['#type'] == 'markup') {
    return format_xml_elements(array(
      $rss_element,
    ));
  }

  // In all other cases return fully-fledged element array. Drupal by default
  // still will not be able to parse it properly (even with help of Views),
  // but this will be managed by our views_rss_handler_field_multiple
  // extending standard CCK's content_handler_field_multiple.
  $element = array(
    '#item' => $variables['#item'],
    '#markup' => format_xml_elements(array(
      $rss_element,
    )),
    '#rss_element' => $rss_element,
  );
  return $element;
}

Functions

Namesort descending Description
views_rss_core_field_formatter_info Implementation of hook_field_formatter_info().
views_rss_core_field_formatter_view No, this is not an implementation of hook_field_formatter_view(). This hook does not exist in D6. This function only more or less simulates it, but in reality it is just a standard theme callback.