We recently began testing the installation profile for Drupal called OpenPublish. The good folks at Phase 2 Technology have opened this installation profile (distribution) up for all to use with some key features for the semantic web. This post is not to go over or compares features with other similar distributions. Instead this is more about how to get the parent feed title into the template for a node created from a feed-item with the feedapi module.
Let’s jump right in. The scenario is that we have a content type of article. The node-article.tpl.php displays the Drupal node author as part of the by line of the article. In our case, we wanted to change this.
Instead, we will have articles created by staff, as well as articles automatically generated from RSS feeds. It is important for us to honor our partners by not showing the article as being authored by us, but instead to change the authored by line to give credit to the originating feed source where appropriate.
Enough talk, let’s see details.
First, we need to make the parent feed’s title and link available as a variable for the templates. We did this in Drupal 6 by adding to the existing template.php file. Using the phptemplate_preprocess_node hook, we can add variables that can be accessed in node templates. (Note: If you are following along in an OpenPublish distribution, this function does not exist in the standard template.php file. You will need to add it.)
Keep in mind that when you use feedapi/feedapi node module to turn a feed item into a node, the $node object does not have all the details about the parent feed that generated the feed-item. It does however contain the nid (node id) of the feed that generated it. Soooo… in our custom code, we need to get this parent feed nid, load the feed title and url and then stuff the link into a variable to use in our node template.
Here is the function as added to the template.php:
/**
* Override or insert PHPTemplate variables into the node templates.
*/
function phptemplate_preprocess_node(&$vars) {
// 20091029 Added for Parent Feed Title Link
if ($vars['node']->feedapi_node->feed_nids) {
$parent_feed_node_id = array_values($vars['node']->feedapi_node->feed_nids);
$parent_feed_node = node_load($parent_feed_node_id[0]);
$vars['parent_feed_link'] = l($parent_feed_node->title, $parent_feed_node->feed->url);
}
}
Just a note that the array index of feed_nids is not zero based but instead, the array index is the same number as the nid. That makes doing the typical $vars['node']->feedapi_node->feed_nids[0] impossible. That is where the php array_values() function comes to the rescue. It takes all the array values from feed_nids and created a zero base indexed array.
Now, we have a variable called $parent_feed_link that is available in our template files. So, our next step is to use it. Since the content type we want to impact is the article type, we are going to modify our node-article.tpl.php. Below is the portion of the file that outputs the article by line:
<div>Article |
<?php print date('m.d.y', $node->created); ?>
| By
<?php
$author = user_load(array('uid' => $node->uid));
if ($parent_feed_link) {
print $parent_feed_link;
}
else {
print l($author->profile_full_name, 'user/'.$node->uid);
}
?>
</div>
As you can see above, we now test to see if the $parent_feed_link actually has a value. If so, we want to output that link. Otherwise, the article was not created from a feed-item and we want to show the actual author of the node.
I know this is simple for many of you, however it took me a little while to figure out where in Drupal 6 to generate these custom variables. In Drupal 5, you may have typically done this in the _phptemplate_variables hook in your template.php.
Well, did we do it wrong? Do you know a better way? Let us know with a comment below.
Blessings.
Popularity: 2% [?]