Question for the group

Events happening in the community are now at Drupal community events on www.drupal.org.
SarahManski's picture

I have a question for you all, we have been building a website for local green business owners to connect with each other. We have an html page that I need to get to be xml. Our super Drupal programmer has not been able to figure out how to get a particular query from our website into an rss feed. It seems like the hook_nodeapi might be a solution if we could figure that out, and the REST server module is way over our heads. Has anyone encountered this before?

Comments

Views

posco's picture

I'm not sure how your data is setup on your site, but if all you want to do is build an RSS feed that displays content from certain Drupal nodes, you can use the Views module:

http://drupal.org/project/views

Using Views, you can setup an RSS feed entirely in the admin interface with no back end programming required. It is one of the most popular Drupal modules (if not the most), so documentation for it is pretty solid. Just Googling I found this guide for setting up RSS with Views: http://drupal.org/node/83597 but it looks pretty lean. You might want to search on the Lullabot website. I'm sure they have a how-to or even a video cast on how to do this.

Sorry

posco's picture

That's actually an outdated article about Views 1 for Drupal 5. A lot of the Views 2 (for Drupal 6) is stored inline with the module and can be exposed with the advanced help module:

http://drupal.org/project/advanced_help

Install that module and enable it with Views to gain access to the inline documentation. They explain this in more detail on the Views project page.

can't use views

jday's picture

We can't use views because the information we need in the feed is the profile nodes with their corresponding flag information but the flag module does not make this information available in views. You can make a view of the nodes a user has flagged but that is not what we need, we need a (view) xml feed of the profile node plus all of the users who have flagged that profile node. I have a query that pulls the needed info and can display it as a page but we need an xml version. That's the hang up.

this is what we have but it only displays the node information, and not the flag information (4 flag types)

function mymodule_rss() {
$nodes = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, fc.uid, fc.fid, u1.picture, r.realname FROM {realname} r, {flag_content} fc LEFT JOIN {node} n ON n.vid = fc.content_id LEFT OUTER JOIN {users} u2 ON u2.uid = fc.uid LEFT OUTER JOIN {users} u1 ON u1.uid = n.uid WHERE n.type = %s AND fc.uid = r.uid AND fc.fid IN (3,4,5,6) ORDER BY n.uid, fc.fid",'profile'), 0, variable_get('feed_default_items', 100));
$channel['title'] = t('!site_name Businesses', array('!site_name' => variable_get('site_name', 'Drupal')));
$channel['link'] = url('profile', array('absolute' => TRUE));

  $items = array();
  while ($row = db_fetch_object($nodes)) {
    $items[] = $row->nid;
  }

  node_feed($items,$channel);

}

I've never used the flag

posco's picture

I've never used the flag module, so I'm not sure how it works. If you have a page callback that can display the information as an HTML page, then it wouldn't be hard to create a new theme function that would theme the same information as an RSS feed instead.

Something like:

<?php
function mymodule_theme() {
  return array(
   
'mymodule_rss_feed' => array(
     
'arguments' => array('nid' => NULL),
    ),
  );
}

function
theme_mymodule_rss_feed($nid) {
 
$obj = node_load($nid);

 
// use $obj->title, $obj->body or any custom CCK fields and wrap them around RSS tags to the spec. and put them in a variable called $out or something.

 
return $out;
}

function
mymodule_custom_page_callback() {
 
$nids = // Run your query and put all the NIDs here

 
print "<xml>"; // Make this a real XML declaration tag, I had to leave out the question mark syntax for the input filter on g.d.o
 
print "<rss>\n"; // Make this a real RSS tag up to specs

 
for ($i = 0; $i < count($nids); $i++) {
    print
theme('mymodule_rss_feed', $nids[$i]);
  }

  print
"</rss>";
  exit;
// Call exit so you prevent Drupal from doing a full page render.
}
?>

So that is a quick and dirty way to do it. Another way would be to make your own Views module in order to get your query into a View. You could also put the XML tags that are in the page callback inside another theme function, so you separate your markup and your PHP even further. You may need to send some custom HTTP headers as well to set the MIME type of the output. Looking into drupal_set_header() to do that:

http://api.drupal.org/api/function/drupal_set_header/6

the catch

jday's picture

Some of the info we need in the feed is not in the node at all, we are pulling that info out of the database with a custom module for the page display. The flag module is set up to collect all of the nodes the user has flagged, but we are using it in the reverse, we want a list of the users that have flagged the node being viewed. (which again is done with the custom module) but now we need that as an xml feed. Can this theme function idea insert non-node info into the $obj?

hook_nodeapi to the rescue

blakehall's picture

Especially if this custom data should always be loaded with nodes of this type, hook_nodeapi() should help provide exactly what you need.

If you'll be at the MadDUG meeting tonight I can show you a quick walk through.

I'll be there

jday's picture

thanks to everyone for your suggestions, sounds like we could be close to a solution

If that is the case, you need

posco's picture

If that is the case, you need to make your own node loading behavior. This can be done with hook_nodeapi():

http://api.drupal.org/api/function/hook_nodeapi/6

When you called node_load(), your hook_nodeapi() will be called with an $op == "load".

Something like this:

<?php
function mymodule_nodeapi(&$node, $op) {
  switch(
$op) {
    case
"load":
      if (
$node->type == // the node type you are targetting) {
       
$node->mycustom_field = // some sort of custom data.
     
}
      break;
  }
}
?>

You can now reference $obj->mycustom_field inside your theme function after the node_load().

I only brought up Views relationships because "looking things up in reverse" is the use-case for Views relationships. It is a very powerful technique. But, it does sound like your situation is a little bit more complicated, so that might not be a solution here.

Also consider

xjm's picture

You could also consider submitting an issue to the Flag module queue (or looking for an existing one). "Views Integration" is its own category for the Flag module, so if data you need isn't exposed for some reason, it might be a feature that someone else using the module would want to patch.
http://drupal.org/project/issues/flag?text=&status=All&priorities=All&ca...

Second that

posco's picture

Yeah, I think that is a great idea too. I also have a hunch that you probably can get to the information you want by using Views relationships. If you posted more details about your setup, then we might be able to help you setup Views to pull the information you want.

What modules are you using and how to you have them setup?

flags, content_profile,

jday's picture

flags, content_profile, realname modules

Flags mod does not expose the uid of the user who did the flagging, these are two values we need.

The information we need in the xml file are as follows:
1. business name (from the profile node)
2. the user who flagged the profile node (from the flag_content table)
3. the flag id (from the flag_content table)
4. link to the user displayed as 'realname' (from the realname table joined with flag content table on uid)

I didn't bring my laptop with

posco's picture

I didn't bring my laptop with me today, so I will mess around with this in the afternoon when I get home and see if Views relationships can be used.

Check out this handbook page

xjm's picture

http://drupal.org/node/326308

Has some suggestions on creating a view with "who flagged this." Sounds like it could be adapted to an XML view perhaps?

Wisconsin

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: