Drupal feeds most of the content out of the box. If it doesn’t do what you want, it allows you to use format_rss_item() and format_rss_channel(). This stuff is simple to use, but not very convenient by itself. There are two things that I like to do when building feeds:
- Use theme functions to format items. (Makes designer happy.)
- Make it as convenient as possible to program new types of RSS items (hello, PHP Metaprogramming).
What’s with that second point?
Yes, I like convenience. Well, say you’re using a feed for getting updates on what your users do on your website. Sort of – an administration feed. Don’t know about you, but I would like to have a nice little function that I can call anywhere in my module, give it all the data, and forget it. Say, on update of some certain content type I wanna post a notice to the admin feed. All I have to do is call one convenient function right there, and relax with Corona, or Guinness if it’s winter.
Step 1. Preparing the RSS items to be published.
Let’s create a table admin_feed that’s gonna hold the feed items. We need 4 columns.- id – an integer (index)
- type – a string (varchar possibly)
- data – text
- created – datetime/timestamp (should default to NOW)
db_query("insert into admin_feed (type, data) values ('%s', '%s')", $type, serialize($data));
}
Step 2. Outputting feed.
Now it’s time to display the feed. There should be a menu item that creates a path to our rss feed.'title' => t('Admin Infostream'),
'callback' => 'foo_feed_admin',
'access' => true,
'type' => MENU_CALLBACK
);
$query = db_query('select * from admin_feed order by created desc');
$items = '';
while ($r = db_fetch_object($query))
$items .= theme('admin_feed', $r);
header(theme('feed_header'));
echo theme('feed_wrapper', $items);
}
I should mention two things about the above function. First of all, look at that loop. It goes through every item in the database, and passes it to the theme(‘admin_feed’) function. We haven’t written it yet, but getting there. Second of all, I have theme(‘feed_header’) and theme(‘feed_wrapper’) for my purposes, but you can make your own theme functions for that. It’s important to see that I’m setting the header and using “echo” to output the result. This result is what you will see when you go to “rss/admin” path.
On to the theme now!
I will call my theme bar. So let’s go to the sites/all/themes/bar directory. I have a template.php file there. Sometimes template.php grows too large with many theming functions for different things. This is why I prefer to split it into multiple files. Thus, for admin feed I created a file called theme_admin_feed.inc and included it into the template.php, but that’s up to you. Now, let’s create the first function – the one that will produce an RSS item.return format_rss_item(
$title,
$link,
$body,
array(
'author' => 'nomail@something.com (admin)',
'guid' => $id
)
);
}
return call_user_func_array('_af_'.$r->type, array( unserialize($r->data), '['. strtoupper($r->type) .']', $r->id) );
}
Step 3. Posting RSS items is now a piece of cake!
So now I want to post a feed item every time someone updates a node in Drupal, including the user who made the update, and whatever else I desire. I have my foo_nodeapi() where I catch the update action, the $node, etc. Here’s what I’ll do in there:global $user;
$node->bywhom = $user;
foo_feed_item('update', $node);
unset($node->bywhom);
break;
$title .= "$node->title was updated by $node->bywhom->name";
$body = "<a href='http://something.com/node/$node->nid/edit'>
http://something.com/node/$node->nid/edit
</a>
";
$link = "http://something.com/node/$node->nid/edit";
return _make_item($title, $body, $link, $id);
}
Maxim,
Sorry for putting this here, but I didn’t see a contact sheet anywhere on your blog, but I wanted to network with you as a client of mine is seeking 2 Ruby on Rails developers (backend, web apps, cms). It’s an incredible company and I thought you might know a couple of people I should talk to.. Will gladly pay a nice referral fee if they get hired. The client who has retained me is confidential but it’s one of the coolest online culture sites out there and they are definitely booming.. We should also connect on linked in.
-Ryan