function views_rss_core_field_formatter_view in Views RSS 7.2
Same name and namespace in other branches
- 6.2 modules/views_rss_core/views_rss_core.field.inc \views_rss_core_field_formatter_view()
Implements hook_field_formatter_view().
This is a dirty trick here. Essentially, we do not want to call a theme function from here, as it should be called from within a view (amongst other to have $view object in $variables). Therefore here we want to return value only, hence use of array('#markup' => $value). However, in some cases it won't be just a simple string value to return, sometimes we'd want to return an array (for example value with additional arguments) - hence the need to serialize it (plus add "serialized" string at the beginning so that our field preprocess function template_preprocess_views_view_views_rss_field() is able to recognize it as serialized array and treat accordingly.
Any better ideas?
File
- modules/
views_rss_core/ views_rss_core.field.inc, line 85 - Field formatters for Views RSS: Core Elements module.
Code
function views_rss_core_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = NULL;
foreach ($items as $delta => $item) {
// Inside a view item may contain NULL data. In that case, just return.
if (($field['type'] == 'file' || $field['type'] == 'image') && empty($item['fid']) || $field['type'] == 'media' && empty($item['file']->fid) || $field['type'] == 'text' && empty($item['safe_value']) || $field['type'] == 'link_field' && empty($item['url'])) {
unset($items[$delta]);
continue;
}
// Get full image URL based on provided image style.
if ($field['type'] == 'image' && !empty($display['settings']['image_style']) && ($image_style = image_style_load($display['settings']['image_style']))) {
$uri = image_style_url($display['settings']['image_style'], $item['uri']);
// Get file size of image preset file (if it has already been created,
// otherwise just create it first and then get file size).
$path = image_style_path($display['settings']['image_style'], $item['uri']);
if (file_exists($path) || image_style_create_derivative($image_style, $item['uri'], $path)) {
$item['filesize'] = filesize($path);
}
}
elseif ($field['type'] == 'media') {
$uri = file_create_url($item['file']->uri);
$item['filesize'] = $item['file']->filesize;
$item['filemime'] = $item['file']->filemime;
}
elseif ($field['type'] == 'text' || $field['type'] == 'link_field') {
$uri = $field['type'] == 'text' ? $item['safe_value'] : $item['url'];
// Great idea to use get_headers() here by Dan Feidt,
// http://drupal.org/user/60005 Thanks Dan!
$headers = get_headers($uri, $format = 1);
$item['filesize'] = $headers['Content-Length'];
$item['filemime'] = $headers['Content-Type'];
}
else {
$uri = file_create_url($item['uri']);
}
// XML element array in format_xml_elements() format.
$rss_element = array(
'key' => 'enclosure',
'attributes' => array(
'url' => $uri,
'length' => $item['filesize'],
'type' => $item['filemime'],
),
);
$element[$delta] = array(
'#item' => $item,
'#markup' => format_xml_elements(array(
$rss_element,
)),
'#rss_element' => $rss_element,
);
}
return $element;
}