Views RSS: fields

Egon Bianchet - November 6, 2006 - 23:00
Project:Views Bonus Pack
Version:5.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs review
Description

What about making views_rss work with fields (like the list view)? It would allow us to create feeds for almost everything ...

#1

merlinofchaos - November 6, 2006 - 23:05

This might be an interesting future feature, tho I'm not sure how it'd work since the translation from a field to an RSS item is not quite straightforward.

#2

Egon Bianchet - November 7, 2006 - 10:18

I was thinking about something like putting the first field in the RSS item title, and the others in the body like a simple definition list.

I'm not sure if it would be better to make two distinct view types, ie "Views RSS: Standard RSS feed" and "Views RSS: Custom RSS feed", or making it work trasparently in case some field was added.

#3

merlinofchaos - November 11, 2006 - 21:46

It would need to be a different plugin type to function. I'd like to see a real use-case where something like this would actually be beneficial. At the moment I'm not certain that it would.

#4

Egon Bianchet - November 13, 2006 - 09:47

It would allow the creation of feeds for all those entities attached to nodes that aren't nodes themselves, like comments, files, taxonomy terms, users, etc. replicating the functionality of modules like Comment RSS and Admin RSS, but allowing for more flexibility.

#5

merlinofchaos - November 13, 2006 - 22:42
Project:Views» Views Bonus Pack
Version:6.x-2.x-dev» 4.7.x-1.x-dev

Ok, I am convinced of the utility.

I don't have the bandwidth to do it; it'd make a nice plugin for the bonus pack or maybe even all by itself.

#6

moshe weitzman - March 6, 2007 - 17:04

could someone post some sample XML for comment RSS or uploads so we can get a feel for whats imagined here? ideally, you will also lis tthe Fields that active in the corresponding View. I think I get it, but want to confirm.

#7

Egon Bianchet - March 8, 2007 - 15:45

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://example.com" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Example.com - Comments</title>
<link>http://example.com</link>
<description>Comments</description>
<language>en</language>
<item>
<title>comment title here (the first field of the view)</title>
<link>http://example.com/node/99#comment-95</link>
<description>comment body and additional view fields here, maybe as list</description>
<pubDate>Wed, 28 Feb 2007 03:38:53 -0800</pubDate>
<dc:creator>John Doe</dc:creator>
<guid isPermaLink="false">comment 95 at http://example.com </guid>
</item>
<item>
</channel>
</rss>

#8

lyricnz - March 18, 2007 - 03:12

And another example, with a file-enclosure constructed from a textfield that contains a URL. Actual node attachments are basically the same (see upload_nodeapi())

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://localhost/drupal51" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>My Test Feed</title>
<link>http://localhost/drupal51/testfeedrss</link>
<description></description>
<language>en</language>

<item>
<title>Test Lesson 1</title>
<link>http://localhost/drupal51/node/1</link>
<description>Test Lesson 1 Body</description>
<comments>http://localhost/drupal51/node/1#comments</comments>
<enclosure url="http://some.where/over/the/rainbow" type="application/x-bittorrent" />
<pubDate>Sun, 18 Mar 2007 11:51:58 +1100</pubDate>
<dc:creator>simon</dc:creator>
<guid isPermaLink="false">1 at http://localhost/drupal51</guid>
</item>

</channel>
</rss>

I got this working by adding the fields in hook_nodeapi (see http://drupal.org/node/104875#comment-212259), but it would nice to be able to do this in the UI.

#9

cpill - May 16, 2007 - 11:58
Version:4.7.x-1.x-dev» 5.x-1.x-dev

A used case for this in the case of Flash widgets. I want to create an RSS view of an image gallery so I can feed it to a Flash image rotator (http://www.jeroenwijering.com/?item=Flash_Image_Rotator). I also need to do this for MP3s in the near future and can think of other cases where it would be invaluable:

- podcasting
- torrent servers
- etc

Basically, being able let to represent any collection of a node type as a specialised RSS feed should be in core as RSS is where its all going. I've looked around and think that, in the combination with the power of views, could open Drupal up int a whole new world of uses.

I will have to hack it in the short term but it should certainly be useful to have a standard way to do this in the long run via the UI perhaps.

#10

cpill - May 16, 2007 - 12:55

A used case for this in the case of Flash widgets. I want to create an RSS view of an image gallery so I can feed it to a Flash image rotator (http://www.jeroenwijering.com/?item=Flash_Image_Rotator). I also need to do this for MP3s in the near future and can think of other cases where it would be invaluable:

- podcasting
- torrent servers
- etc

Basically, being able let to represent any collection of a node type as a specialised RSS feed should be in core as RSS is where its all going. I've looked around and think that, in the combination with the power of views, could open Drupal up int a whole new world of uses.

I will have to hack it in the short term but it should certainly be useful to have a standard way to do this in the long run via the UI perhaps.

#11

joachim - February 25, 2009 - 16:23
Status:active» needs review

I've just written a views style plugin that does this.
Field labels are used to make the XML elements.
Labels "title", "body", "link" are special and used to make the title, link, description in the XML.

Could do with a validator to check that that those three labels exist.

/**
* Views style theming function for list RSS view.
* Field labels are used as XML element names; if the label is missing the (ugly!) queryname is used.
* Set which fields to use for title, description, and link by giving them labels
* "title", "body", "link" respectively
*/          
function theme_view_list_rss($view, $nodes, $type) {
  global $base_url;

  $channel = array(
    // a check_plain isn't required on these because format_rss_channel
    // already does this.
    'title'       => views_get_title($view, 'page'),
    'link'        => url($view->feed_url ? $view->feed_url : $view->real_url, NULL, NULL, true),
    //'description' => $view->description,
  );

  $namespaces = array('xmlns:dc="http://purl.org/dc/elements/1.1/"');
 
  $item_length = variable_get('feed_item_length', 'teaser');
 
  $fields = _views_get_fields();

  #dsm($view->field);
  // Set up some primary fields, eg title and body and link
  // we should only have ONE candidate for each of these
  // if not, bad stuff happens
  // primary fields are marked by having a label that is one of:
  // "title", "body", "link"
  // This takes care of removing them from other processed fields
  // and giving them special status in the call to format_rss_item.
  foreach ($view->field as $id => $field) {
    //if (in_array($field['id'], array('rss:title', 'rss:body', 'rss:link'))) {
    if ($field['label'] == 'title') {
      $field_special['title'] = $field;
      $field_special['title']['options'] = 'nolink'; // force no link on title.
      unset($view->field[$id]);
    }
    if ($field['label'] == 'body') {
      $field_special['body'] = $field;
      unset($view->field[$id]);
    }
    if ($field['label'] == 'link') {
      $field_special['link'] = $field;
      unset($view->field[$id]);
    }
  } 
 
  #dsm($view->field);
  foreach ($nodes as $node) {   
    #dsm($node);
   
    if (isset($field_special['title'])) {
      $title = views_theme_field('views_handle_field', $field_special['title']['queryname'], $fields, $field_special['title'], $node, $view);
    }
    if (isset($field_special['body'])) {
      $body =  views_theme_field('views_handle_field', $field_special['body']['queryname'], $fields, $field_special['body'], $node, $view);
    }
    $link = url('node/'. $node->$field_special['link']['queryname'], NULL, NULL, 1);
   
    $extra = array();
    foreach ($view->field as $field) {
      if (!isset($fields[$field['id']]['visible']) && $fields[$field['id']]['visible'] !== FALSE) {
        // use $field['field'] as the XML key unless there is a label.
        if (isset($field['label'])) {
          $key = $field['label'];
        }
        else {
          $key = $field['field'];         
        }
       
        $extra[$key] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
      }
    }
   
    /*
    // test zone:
    $item = format_rss_item($title, $link, $body, $extra);
    dsm($item);
    */
   
    $items .= format_rss_item($title, $link, $body, $extra);
  }

  // test
  // return $items;

  $channel_defaults = array(
    'version'     => '2.0',
    'title'       => variable_get('site_name', 'drupal') .' - '. variable_get('site_slogan', ''),
    'link'        => $base_url,
    'description' => variable_get('site_mission', ''),
    'language'    => $GLOBALS['locale'],
  );
  $channel = array_merge($channel_defaults, $channel);

  $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  $output .= "<rss version=\"". $channel["version"] . "\" xml:base=\"". $base_url ."\" ". implode(' ', $namespaces) .">\n";
  $output .= format_rss_channel($channel['title'], $channel['link'], $channel['description'], $items, $channel['language']);
  $output .= "</rss>\n";

  drupal_set_header('Content-Type: text/xml; charset=utf-8');
  print $output;
  module_invoke_all('exit');
  exit;
}

#12

wundo - September 12, 2009 - 01:27

#13

gausarts - September 27, 2009 - 11:49

A very old thread and still survives. Subscribing. Thanks.

#14

ccshannon - October 24, 2009 - 00:58

subscribe

#15

vitis - November 4, 2009 - 16:32

subscribing

 
 

Drupal is a registered trademark of Dries Buytaert.