Hi

This is more like a feature suggestion than a request..

I've tried adding the possibilty to add enclosure tags (with images) to the rss feeds created with view_rss..

I think it would be a nice feature to an otherwise great module..

My code is a bit sketchy, but I hope it could get anyone started. Unfortunately I don't know how to make patches so, I have tried to paste the snippets here.. (see below)

Let me know if you are interested in this feature and if you will try to implement it. I'm not experienced in developing modules, but if there's more that I can do, please let me know

regards

Ole

in views_plugin_style_rss_fields.inc line 166 add this:

      'enclosure' => array(
        'title' => t('Enclosure'),
        'description' => t('RSS 2.0 enclosure element'),
      ),

then in views_rss_views_fields.theme.inc line 68 add this, to make the enclosure tag be formatted in another way:

  //remove enclosure from item array, because it should be formatted in another way
  if(isset($item['enclosure']))
  {
  	$row .= $item['enclosure'];
  	unset($item['enclosure']);
  }

and in views_rss.module add this code to make a cck widget for filefield and images, so that it will output the enclosure tag instead of img tag..


function views_rss_theme()
{

  /* merge this with original views_rss code */

  $theme = array();
  $theme['views_rss_formatter_rss_image'] = array(
      'arguments' => array('element' => NULL),
      'function' => 'theme_views_rss_formatter_rss',
    );

  foreach (imagecache_presets() as $preset) {
    $theme['views_rss_formatter_'. $preset['presetname'] .'_rss'] = array(
      'arguments' => array('element' => NULL),
      'function' => 'theme_views_rss_formatter_rss',
    );
  }

  return $theme;
	
}

function views_rss_field_formatter_info() {
  $formatters = array();
  
  $formatters['rss_image'] = array(
    'label' => t('RSS : image rss'),
    'field types' => array('image', 'filefield'),
  );
    
  foreach (imagecache_presets() as $preset) {
    $formatters[$preset['presetname'] .'_rss'] = array(
      'label' => t('RSS : @preset image rss', array('@preset' => $preset['presetname'])),
      'field types' => array('image', 'filefield'),
    );
  }
  return $formatters;
}

function theme_views_rss_formatter_rss($element) {
  // Inside a view $element may contain NULL data. In that case, just return.
  //dpm($element);
  if (empty($element['#item']['fid'])) {
    return '';
  }
  
  $presetname = substr($element['#formatter'], 0, strrpos($element['#formatter'], '_'));

  $item = $element['#item'];
  
  if(in_array($presetname,imagecache_presets()))
    $path = imagecache_create_url($presetname, $item['filepath']);
  else
    $path = $item['filepath'];
    
  return "<enclosure url='". url($path, array('absolute'=>TRUE)) ."' length='{$item['filesize']}' type='{$item['filemime']}' />";
}

Comments

noslokire’s picture

+1 Looking for the same thing on the same day, what are the chances of that! I'll take a look at the code as well...

greenmachine’s picture

FYI, this *almost* worked verbatim for me. I had to change theme_views_rss_formatter_rss() because imagecache_presets() in my version of imagecache (1.6) does not return what the above code expects (a one-dimensional array). Instead it returns an array of arrays.

persand’s picture

Version: 6.x-1.0-beta4 » 6.x-1.x-dev
Status: Active » Needs review
StatusFileSize
new5.18 KB

Here's a patch for the current 6.x-1.0-dev. Had to modify it a bit to make it work after the namespacing handling was refactored in the latest release.

It's been tested and is being used in production on www.medievarlden.se.

It currently only supports one enclosed file but since the RSS standard supports multiple files that probably should be added in future versions.

dixon_’s picture

StatusFileSize
new5.21 KB

Here is a slight modification of Per's patch in #3. This patch properly renders the imagecache presets in the enclosure tag.

This code:

+++ views_rss.module	16 Sep 2010 11:45:28 -0000
@@ -15,23 +15,86 @@ function views_rss_views_api() {
+  if (in_array($presetname, imagecache_presets())) {
+    $path = imagecache_create_url($presetname, $item['filepath']);
+  }

Should be:

+++ views_rss.module	16 Sep 2010 11:45:28 -0000
@@ -15,23 +15,86 @@ function views_rss_views_api() {
+  $presets = imagecache_presets();
+
+  if (in_array($presetname, array_keys($presets))) {
+    $path = imagecache_create_url($presetname, $item['filepath']);
+  }

Powered by Dreditor.

hexblot’s picture

I couldn't get this to work properly --

after installing the -dev version, and applying the latest patch, I did see the enclosure field in the style settings, however once selecting a field it was still being output as an <img> tag instead of an <enclosure> .

From what I could tell, the function that outputs the enclosure (theme_views_rss_formatter_rss in views_rss.module) was never called. I ended up using a views template for the specific field that outputs what is needed, but I *really* dont like that approach.

How can I assist with debugging this?

mikeytown2’s picture

I get img tag as well. This has the specs for images in rss
http://perishablepress.com/press/2007/02/04/feed-your-image-via-atom-or-...

anyway it appears that views wants a tpl file, not a function. going to work on a patch to get this working correctly.

peximo’s picture

@hexblot, @mikeytown2: you should select the RSS formatter in the configuration of the field you select as enclosure.

bleen’s picture

subscribing

hexblot’s picture

StatusFileSize
new5.3 KB

@peximo -- thanks for the tip!

digging into this, there is also a bug with imagecache support in patch #4, don't know if it was created by an update in imagegecache. Thing is, the included code will always fail to detect imagecache since the array keys are now numeric, and do not include the preset name. Solution is to replace the loop

  if (in_array($presetname, array_keys($presets))) {

located in views_rss.module line ~88 with

  $valid = false;
  foreach($presets as $preset) {
    if ( $presetname == $preset['presetname'] ) $valid = true;
  }
  if($valid) {

This is a bit slower but will work just fine. You can find the fixed patch attached. Do note that the metadata ( filesize, filemime ) still reflect on the original image, not imagecache. Personally it doesn't bother me though.

You can download the new patch below ( do not apply this if already have applied another patch ).

maciej.zgadzaj’s picture

Status: Needs review » Closed (fixed)

Support for enclosure tag based on above patches added to most recent 6.x-1.x-dev version (from 2011-Nov-09). D7 version to follow.

Also, added first draft of module documentation - please read for what it can and can't do.