drupal_add_feed

cschults - June 19, 2009 - 00:46

I've added the function drupal_add_feed to my template.php file and it is adding the link to the head section of my HTML. However, the feed icon is not being printed on my page as expected. I have the line

<?php
print $feed_icons;
?>
within my page.tpl.php file.

One, should I expect drupal_add_feed to add the icon?

Two, is making the call to drupal_add_feed in template.php the correct place?

Solved problem, but would still like to know

cschults - June 25, 2009 - 17:00

I was able to achieve the desired effect by using phptemplate_preprocess_page(), but I would still like to know about the proper usage of drupal_add_feed(). Anyone?

Thanks!

Feed Icons

Karlheinz - June 25, 2009 - 17:10

One, should I expect drupal_add_feed to add the icon?

The Drupal API is your friend:
http://api.drupal.org/api/function/drupal_add_feed

Looking at the code, it should add the icon (via theme()):
$stored_feed_links[$url] = theme('feed_icon', $url, $title);

Two, is making the call to drupal_add_feed in template.php the correct place?

Probably not. If all you want to do is add the feed icon to the page, do that in Drupal itself by putting the "Syndicate" block into a region. No need to hard-code it into a theme.

-Karlheinz

Thanks Karlheinz

cschults - June 25, 2009 - 18:02

I did review the API and saw that line of code, which I thought meant it would add the icon.

However, that was not the case. While it was adding the link element to my head code, the icon was not being printed, even though I had print $feed_icons in my theme's page template.

The reason it did not work was perhaps the placement. I put it at the end my theme's template.php file (not in a function). which is where I also add JavaScript and CSS (and often wondered if this is the correct place). This at least added the link element mentioned above. When I tried adding to my page template, nothing happened. Perhaps the variable 'feed_icons' was getting overwritten during a later processing step???

As for the why, the feeds are for my site, not external ones (FYI). I'm using FeedBurner, so I can't use the URLs that Drupal generates. Plus, some node types have different feeds, so I need to determine which feed to add depending on the node's attributes.

Hmmm

Karlheinz - June 25, 2009 - 19:34

I put it at the end my theme's template.php file (not in a function). which is where I also add JavaScript and CSS (and often wondered if this is the correct place).

No, indeed it is not.

It sounds like you haven't fully digested Drupal theming. You should really read everything linked from here:
http://drupal.org/node/171194

Briefly:
- CSS and JavaScripts should be put in their own files
- Link to these files in the YOURTHEME.info file, using the stylesheets[all][] and scripts[] arrays
- The template.php file is for overriding theme functions (usually found in Drupal's core theme.inc file), and for preprocessors (PHP that creates variables to be passed to template files), and that's all
- HTML should be in *.tpl.php files - page-level HTML is in page.tpl.php, and you can also override HTML using node.tpl.php, block.tpl.php, etc.

Also, I'm not exactly sure what you want now. You mean you want to create a link to an RSS/Atom feed for your own site, to read using Google's FeedBurner? You want, in other words, to syndicate your site? I don't see why my suggestion wouldn't work, then...?

-Karlheinz

Use of feeds, JavaScript and CSS

cschults - June 25, 2009 - 23:23

For RSS feeds, I have various content types, of which blog post is one of them. One of the fields in a blog post is blog, which is for the user to specify which blog a post belongs to. This blog field is used in a variety of ways, one of which is to determine which RSS feed to attach to the blog post.

Thus, I have created a feed view, which accepts a blog name as an argument. For each possible blog feed, I've set up a FeedBurner feed, which is the link I want to include on my blog posts.

For example:

Source feed = http://www.example.com/blogs/blogname/feed
FeedBurner feed = http://feeds.example.com/blogname

So, I need to instruct Drupal to use the FeedBurner URL in the link element in the head and for linking the feed icon. I was trying to use drupal_add_feed(), but ran into the aforementioned problem. But did manage to do this be setting/updating the page variables.

Now extend this to all the different content types (blogs, videos, recipes, etc), which each have their own feed(s).

The same goes for CSS and JavaScript specific to content types. For example, on my video pages, and just my video pages, I need to add JavaScript (both inline and link to external file). To accomplish this, I'm using the drupal_add_js(). Likewise, I use drupal_add_css() to add a link to an external style sheet.

Now both of these functions are put at the bottom of my template.php and work fine. But if someone would like to explain where the correct place to use these functions, I would certainly appreciate it!

If you use the FeedBurner

Dave Reid - June 26, 2009 - 14:41

If you use the FeedBurner module with the Url alter module you can have all links to configurable local feeds on your site automatically replaced with the FeedBurner feeds.

...

Jeff Burnz - June 27, 2009 - 20:51

hmmm, what a handy tip, great to know!

For RSS feeds, I have various

Karlheinz - June 27, 2009 - 16:48

For RSS feeds, I have various content types, of which blog post is one of them. One of the fields in a blog post is blog, which is for the user to specify which blog a post belongs to. This blog field is used in a variety of ways, one of which is to determine which RSS feed to attach to the blog post.

Wow, try saying THAT ten times fast.

This is all a bit over my head, but it sounds like using the FeedBurner module would help out tremendously.

But as for this:

For example, on my video pages, and just my video pages, I need to add JavaScript (both inline and link to external file). To accomplish this, I'm using the drupal_add_js(). Likewise, I use drupal_add_css() to add a link to an external style sheet.

I would suggest doing this using template suggestions:
http://drupal.org/node/223440

You could either add the JavaScript and CSS in each tpl.php file, or (my recommendation) use different DIV and SPAN classes for the styles you want, and manipulate those with seperate JS/CSS files you import in your .info file.

You can also look into theming each CCK field - it's in the Advanced Help, but not on drupal.org yet, so I can't point you to a specific page.

Your mileage will vary, depending upon exactly how you set up your video, recipe, etc. pages.

-Karlheinz

Necropost...

Karlheinz - July 2, 2009 - 23:46

It's a bit late, but I just found out about these two modules. They allow you to inject CSS and JavaScript into your pages, without having to do custom theming or coding:
http://drupal.org/project/css_injector
http://drupal.org/project/js_injector

-Karlheinz

Correct usage of drupal_add_js and drupal_add_css

cschults - July 28, 2009 - 20:21

I believe I finally figured out the correct usage of drupal_add_js and drupal_add_css. The key is to also use drupal_get_js and drupal_get_css, and put everything in your page preprocess function.

For JavaScript, you first add your custom Javascript using drupal_add_js, then do this:
$vars['scripts'] = drupal_get_js();

Likewise for CSS: add custom styles using drupal_add_css, then:
$vars['styles'] = drupal_get_css();

Someone please correct me if I'm wrong, but it works!

It's easier to use the .info file

Karlheinz - July 29, 2009 - 01:30

If you're using Drupal 6, it's easier to just include that in the theme's .info file, like so:

stylesheets[all][] = styles.css
scripts[] = script.js

That puts the CSS and JavaScript files on all pages of your site, though.

If you only want certain pages to have it, then you could also put it in the HTML headers of page.tpl.php (or page-blog.tpl.php or whatever).

-Karlheinz

Conditional inclusion of JavaScript and CSS

cschults - August 7, 2009 - 03:08

Karlheinz, yes, I need to conditionally add JavaScript and CSS depending on various factors. Thanks though!

I've got a Zen sub theme and

j0rd - August 21, 2009 - 21:21

I've got a Zen sub theme and using drupal_add_feed in template.php preprocess_page wasn't working for me.

I had to do this:

<?php
drupal_add_feed
(url('feed.xml'), t('My Feed'));
$vars['head'] = drupal_get_html_head();
?>

Perfect!

cschults - August 24, 2009 - 18:20

Thanks j0rd! That works.

 
 

Drupal is a registered trademark of Dries Buytaert.