You are here

views_xhtml.module in Views Datasource 7

Same filename and directory in other branches
  1. 5 views_xhtml.module
  2. 6 views_xhtml.module

Module definition for views_xhtml

File

views_xhtml.module
View source
<?php

/**
 * @file
 * Module definition for views_xhtml
 *
 * @see views_xhtml.views.inc
 */

/**
 * Implements hook_views_api().
 */
function views_xhtml_views_api() {
  return array(
    'api' => '2.0',
    'path' => drupal_get_path('module', 'views_xhtml') . '/views',
  );
}

/**
 * We almost duplicate the content_handler_field_multiple::render function
 * to get the multiple rendered field values in an array
 * @param $field
 * @param $values
 * @return unknown_type
 */
function _views_xhtml_render_multiple_field($field, $values) {
  $options = $field->options;

  // If this is not a grouped field, use content_handler_field::render().
  if (!$field->defer_query) {
    return $field
      ->render($values);
  }

  // We're down to a single node here, so we can retrieve the actual field
  // definition for the node type being considered.
  $content_field = content_fields($field->content_field['field_name'], $values->{$field->aliases['type']});
  $vid = $values->{$field->field_alias};
  if (isset($field->field_values[$vid])) {

    // Gather items, respecting the 'Display n values starting from m' settings.
    $count_skipped = 0;
    $items = array();
    foreach ($field->field_values[$vid] as $item) {
      if (empty($options['multiple']['multiple_from']) || $count_skipped >= $options['multiple']['multiple_from']) {
        if (empty($options['multiple']['multiple_number']) || count($items) < $options['multiple']['multiple_number']) {

          // Grab the nid - needed for render_link().
          $nid = $item['_nid'];
          unset($item['_nid']);
          $items[] = $item;
        }
        else {
          break;
        }
      }
      $count_skipped++;
    }

    // Build a pseudo-node from the retrieved values.
    $node = drupal_clone($values);

    // content_format and formatters will need a 'type'.
    $node->type = $values->{$field->aliases['type']};
    $node->nid = $values->{$field->aliases['nid']};
    $node->vid = $values->{$field->aliases['vid']};

    // Some formatters need to behave differently depending on the build_mode
    // (for instance: preview), so we provide one.
    $node->build_mode = NODE_BUILD_NORMAL;

    // Render items.
    $formatter_name = $options['format'];
    if ($items && ($formatter = _content_get_formatter($formatter_name, $content_field['type']))) {
      $rendered = array();
      if (content_handle('formatter', 'multiple values', $formatter) == CONTENT_HANDLE_CORE) {

        // Single-value formatter.
        $n = 0;
        foreach ($items as $item) {
          $output = content_format($content_field, $item, $formatter_name, $node);
          if (!empty($output)) {
            $rendered[++$n] = $field
              ->render_link($output, (object) array(
              'nid' => $nid,
            ));
          }
        }
      }
      else {

        // Multiple values formatter.
        $output = content_format($content_field, $items, $formatter_name, $values);
        if (!empty($output)) {
          $rendered[++$n] = $field
            ->render_link($output, (object) array(
            'nid' => $nid,
          ));
        }
      }
      if (count($rendered) > 1) {

        // TODO: could we use generic field display ?

        //return theme('content_view_multiple_field', $rendered, $content_field, $values);
        return $rendered;
      }
      elseif ($rendered) {
        return $rendered[1];
      }
    }
  }
  return '';
}

/**
 * Takes each field from a row object and renders the field as determined by the field's theme
 *
 * @param $view
 *   View the row belongs to
 * @param $row
 *   Row object
 * @return array
 *   Object containing all the raw and rendered fields
 */
function _views_xhtml_render_fields($view, $row) {
  $field_ids = array_keys($view->field);
  $rendered_fields = array();
  foreach ($field_ids as $id) {
    $field = $view->field[$id];
    $field_is_multiple = FALSE;
    $field_raw = array();
    if (isset($field->options['multiple']['group']) && isset($field->field_values)) {
      $field_output = _views_xhtml_render_multiple_field($field, $row);
      $n = 0;
      if (is_array($field_output)) {
        foreach ($field->field_values[$row->{$field->field_alias}] as $item) {
          $field_raw[++$n] = $item["value"];
        }
        $field_is_multiple = TRUE;
      }
      else {
        $field_raw = $view->field[$field->options['id']]
          ->advanced_render($row);
      }
    }
    else {
      $field_output = $view->field[$field->options['id']]
        ->advanced_render($row);
      $field_raw = isset($view->field[$id]->field_alias) && isset($row->{$view->field[$id]->field_alias}) ? $row->{$view->field[$id]->field_alias} : NULL;
    }
    $img_match = array();
    $src_match = array();
    if (is_array($field_output)) {
      foreach ($field_output as $i => $f) {
        if (preg_match("/<img[^>]+>/i", $f, $img_match)) {
          if (preg_match('/(src)="([^"]*)"/i', $img_match[0], $src_match)) {
            $field_output[$i] = $src_match[2];
          }
        }
      }
    }
    else {
      if (preg_match("/<img[^>]+>/i", $field_output, $img_match)) {
        if (preg_match('/(src)="([^"]*)"/i', $img_match[0], $src_match)) {
          $field_output = $src_match[2];
        }
      }
    }
    if (empty($field->options['exclude']) && $field_output != "" && !empty($field_output)) {
      $object = new stdClass();
      $object->id = $id;
      $object->content = $field_output;
      $object->raw = $field_raw;
      $object->class = drupal_clean_css_identifier(strtolower($id));
      $object->label = check_plain($view->field[$id]
        ->label());
      $object->is_multiple = $field_is_multiple;
      $rendered_fields[$id] = $object;
    }
  }
  return $rendered_fields;
}

/**
 * Replaces illegal characters in a attribute value string with their encoded entities as well as the " char.
 *
 * @param $input
 *   String to process.
 * @return
 *   String with illegal characters stripped away and entities encoded.
 */
function _views_xhtml_strip_illegal_xml_attribute_value_chars($input) {
  $output = str_replace('<', '&lt;', $input);

  // encode left-angled bracket
  $output = str_replace('>', '&gt;', $output);

  // encode right-angled bracket
  $output = str_replace('"', '&quot;', $output);

  // encode quote
  return $output;
}

/**
 * Strips characters not matching the xhtml Name production:
 *
 * @param $input
 *   String to process.
 * @return
 *   String with illegal characters stripped.
 */
function _views_xhtml_strip_illegal_xml_name_chars($input) {
  $output = preg_replace("/(^xhtml)|([^A-Za-z0-9_\\-\\.:])/", "", $input);
  return $output;
}

/**
 * Creates an author object to use in the headers of OPML and Atom documents
 * @param $username
 * @return unknown_type
 */
function _views_xhtml_format_author($username) {
  $author = array();
  if (!$username) {
    $author["name"] = variable_get('anonymous', 'Anonymous');
    $author["email"] = variable_get('site_mail', 'none@none.info');
    return $author;
  }
  if (is_numeric($username)) {
    $user = user_load($username);
  }
  else {
    $user = user_load_by_name($username);
  }
  if (!$user) {

    //$author["name"] = variable_get('anonymous', ('Anonymous'));

    //$author["name"] = variable_get('anonymous', ('Anonymous'));
    $author["name"] = $username;
    $author["email"] = variable_get('site_mail', 'none@none.info');
    return $author;
  }
  if (!module_exists("profile")) {
    $author["name"] = $username;
    $author["email"] = check_plain($user->mail);
    return $author;
  }
  else {
    profile_load_profile($user);
    $author["name"] = check_plain($user->profile_name ? $user->profile_name : $username);
    $author["email"] = check_plain($user->mail);
    if (user_access('access user profiles')) {
      $author['uri'] = url('user/' . $user->uid, array(
        'absolute' => TRUE,
      ));
    }
    return $author;
  }
}

/**
 * If input is a serialized date array, return a date string
 *
 * @param $input
 *   Input to check.
 * @return
 *   Either the original input or a date string.
 */

/** may never be used as the properly rendered date strings are available
 * function views_xhtml_is_date($input) {
 * if (strpos($input, 'a:3:{s:5:"month"') !== 0) {
 * return $input;
 * }
 * else {        // serialized date array
 * $date = unserialize($input);
 * return format_date(mktime(0, 0, 0, $date['month'], $date['day'], $date['year']), 'custom', DATE_ISO8601);
 * }
 * }
 **/
function _views_xhtml_debug_stop($var) {
  var_dump($var);
  module_Invoke_all('exit');
  exit;
}

/**
 * Strips illegal Unicode characters and encodes entities in string.
 *
 * @param $input
 *   String to process.
 * @return
 *   String with illegal characters stripped away and entities encoded.
 */

//function views_xhtml_strip_illegal_chars($input) {

//  $output = preg_replace(
//              '/[\x{80}-\x{A0}'.      // Non-printable ISO-8859-1 + NBSP
//              '\x{01}-\x{1F}'.        // Non-printable ASCII characters
//              '\x{AD}'.               // Soft-hyphen
//              '\x{2000}-\x{200F}'.    // Various space characters
//              '\x{2028}-\x{202F}'.    // Bidirectional text overrides
//              '\x{205F}-\x{206F}'.    // Various text hinting characters
//              '\x{FEFF}'.             // Byte order mark
//              '\x{FF01}-\x{FF60}'.    // Full-width latin
//              '\x{FFF9}-\x{FFFD}'.    // Replacement characters
//              '\x{0}]/u',             // NULL byte
//            '', $input);
//  $output = str_replace('"', '&quot;', $output);      // encode quote
//  $output = str_replace('&', '&amp;', $output);       // encode ampersand
//  $output = str_replace("'", '&apos;', $output);      // encode apostrophe
//  $output = str_replace('<', '&lt;', $output);        // encode left-angled bracket
//  $output = str_replace('>', '&rt;', $output);        // encode right-angled bracket
//  return $output;

//}

/**
 * If input is a serialized date array, return a date string
 *
 * @param $input
 *   Input to check.
 * @return
 *   Either the original input or a date string.
 */
function _views_xhtml_is_date($input) {
  if (strpos($input, 'a:3:{s:5:"month"') !== 0) {
    return $input;
  }
  else {

    // serialized date array
    $date = unserialize($input);
    return format_date(mktime(0, 0, 0, $date['month'], $date['day'], $date['year']), 'custom', DATE_ISO8601);
  }
}

Functions

Namesort descending Description
views_xhtml_views_api Implements hook_views_api().
_views_xhtml_debug_stop may never be used as the properly rendered date strings are available function views_xhtml_is_date($input) { if (strpos($input, 'a:3:{s:5:"month"') !== 0) { return $input; } else { // serialized date array $date =…
_views_xhtml_format_author Creates an author object to use in the headers of OPML and Atom documents
_views_xhtml_is_date If input is a serialized date array, return a date string
_views_xhtml_render_fields Takes each field from a row object and renders the field as determined by the field's theme
_views_xhtml_render_multiple_field We almost duplicate the content_handler_field_multiple::render function to get the multiple rendered field values in an array
_views_xhtml_strip_illegal_xml_attribute_value_chars Replaces illegal characters in a attribute value string with their encoded entities as well as the " char.
_views_xhtml_strip_illegal_xml_name_chars Strips characters not matching the xhtml Name production: