views_xml.module in Views Datasource 6
Same filename and directory in other branches
Module definition for views_xml
See also
File
views_xml.moduleView source
<?php
/**
* @file
* Module definition for views_xml
*
* @see views_xml.views.inc
*/
function views_xml_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'views_xml'),
);
}
/**
* 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_xml_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])) {
$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_xml_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_xml_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']]
->theme($row);
}
}
else {
$field_output = $view->field[$field->options['id']]
->theme($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 = views_css_safe($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_xml_strip_illegal_xml_attribute_value_chars($input) {
$output = str_replace('<', '<', $input);
// encode left-angled bracket
$output = str_replace('>', '>', $output);
// encode right-angled bracket
$output = str_replace('"', '"', $output);
// encode quote
return $output;
}
function _views_xml_xmlEntities($str) {
$xml = array(
'"',
'&',
'&',
'<',
'>',
' ',
'¡',
'¢',
'£',
'¤',
'¥',
'¦',
'§',
'¨',
'©',
'ª',
'«',
'¬',
'­',
'®',
'¯',
'°',
'±',
'²',
'³',
'´',
'µ',
'¶',
'·',
'¸',
'¹',
'º',
'»',
'¼',
'½',
'¾',
'¿',
'À',
'Á',
'Â',
'Ã',
'Ä',
'Å',
'Æ',
'Ç',
'È',
'É',
'Ê',
'Ë',
'Ì',
'Í',
'Î',
'Ï',
'Ð',
'Ñ',
'Ò',
'Ó',
'Ô',
'Õ',
'Ö',
'×',
'Ø',
'Ù',
'Ú',
'Û',
'Ü',
'Ý',
'Þ',
'ß',
'à',
'á',
'â',
'ã',
'ä',
'å',
'æ',
'ç',
'è',
'é',
'ê',
'ë',
'ì',
'í',
'î',
'ï',
'ð',
'ñ',
'ò',
'ó',
'ô',
'õ',
'ö',
'÷',
'ø',
'ù',
'ú',
'û',
'ü',
'ý',
'þ',
'ÿ',
);
$html = array(
'"',
'&',
'&',
'<',
'>',
' ',
'¡',
'¢',
'£',
'¤',
'¥',
'¦',
'§',
'¨',
'©',
'ª',
'«',
'¬',
'­',
'®',
'¯',
'°',
'±',
'²',
'³',
'´',
'µ',
'¶',
'·',
'¸',
'¹',
'º',
'»',
'¼',
'½',
'¾',
'¿',
'À',
'Á',
'Â',
'Ã',
'Ä',
'Å',
'Æ',
'Ç',
'È',
'É',
'Ê',
'Ë',
'Ì',
'Í',
'Î',
'Ï',
'Ð',
'Ñ',
'Ò',
'Ó',
'Ô',
'Õ',
'Ö',
'×',
'Ø',
'Ù',
'Ú',
'Û',
'Ü',
'Ý',
'Þ',
'ß',
'à',
'á',
'â',
'ã',
'ä',
'å',
'æ',
'ç',
'è',
'é',
'ê',
'ë',
'ì',
'í',
'î',
'ï',
'ð',
'ñ',
'ò',
'ó',
'ô',
'õ',
'ö',
'÷',
'ø',
'ù',
'ú',
'û',
'ü',
'ý',
'þ',
'ÿ',
);
$str = str_replace($html, $xml, $str);
$str = str_ireplace($html, $xml, $str);
return $str;
}
/**
* Strips characters not matching the XML Name production:
*
* @param $input
* String to process.
* @return
* String with illegal characters stripped.
*/
function _views_xml_strip_illegal_xml_name_chars($input) {
$output = preg_replace("/(^xml)|([^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_xml_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(array(
'uid' => $username,
));
}
else {
$user = user_load(array(
'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_xml_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_xml_debug_stop($var) {
var_dump($var);
module_Invoke_all('exit');
exit;
}
Functions
Name | Description |
---|---|
views_xml_views_api | @file Module definition for views_xml |
_views_xml_debug_stop | may never be used as the properly rendered date strings are available function views_xml_is_date($input) { if (strpos($input, 'a:3:{s:5:"month"') !== 0) { return $input; } else { // serialized date array $date =… |
_views_xml_format_author | Creates an author object to use in the headers of OPML and Atom documents |
_views_xml_render_fields | Takes each field from a row object and renders the field as determined by the field's theme |
_views_xml_render_multiple_field | We almost duplicate the content_handler_field_multiple::render function to get the multiple rendered field values in an array |
_views_xml_strip_illegal_xml_attribute_value_chars | Replaces illegal characters in a attribute value string with their encoded entities as well as the " char. |
_views_xml_strip_illegal_xml_name_chars | Strips characters not matching the XML Name production: |
_views_xml_xmlEntities |