Images in Drupal RSS Feed, without additional modules

There is an easier way to include the image associated with your node in a Drupal Views RSS feed. No additional modules are required.

Firstly you need to add the Media RSS namespace to your views-view-rss.tpl.php:

<rss version="2.0" xml:base="<?php print $link; ?>"<?php print $namespaces; ?> xmlns:media="http://search.yahoo.com/mrss/">

Secondly, in the row style template for your RSS feed (mine is called views-view-row-rss–market-news-rss-feed–feed.tpl.php), you need add a media element, and enter your image field name, your image style, and your dimensions:

<?php

/**
 * @file
 * Default view template to display a item in an RSS feed.
 *
 * @ingroup views_templates
 */
?>
  <item>
    <title><?php print $title; ?></title>
    <link><?php print $link; ?></link>
    <description><?php print $description; ?></description>
    <media:content url="<?php print image_style_url('news_old_style__300x250_', $node->field_image['und'][0]['uri']); ?>" type="image/jpeg" medium="image" height="250" width="300" lang="en" />
    <?php print $item_elements; ?>
  </item>

Lastly, make the field data available to the template above by adding the following to your template.php

function nadex_preprocess_views_view_row_rss(&$vars) {
  $view     = &$vars['view'];
  $options  = &$vars['options'];
  $item     = &$vars['row'];
 
  // Use the [id] of the returned results to determine the nid in [results]
  $result = &$vars['view']->result;
  $id   = &$vars['id'];
  $node   = node_load( $result[$id-1]->nid );
  $vars['node'] = $node;
}

Thanks to these guys for the inspiration:

Published
Categorized as Drupal, RSS

Drupal: How to access fields in html.tpl.php

When developing my AMP subtheme for Drupal 7, I wanted to only include the amp-youtube extension if the current page actually contained a YouTube video. The amp-youtube extension javascript can only be included in the HEAD of the HTML document, so in Drupal, your html.tpl.php must know about content which is usually only available at node.tpl.php level. Here is the solution:

In your template.php

function MYTHEME_preprocess_html(&$vars) {
//we need to know if the page contains a YouTube video, so we create the variable below and reference it in html.tpl.php
if ($node = menu_get_object()) {
$vars['youtube_video_id'] = $node->field_youtube_video_id;
}
}

in your html.tpl.php

<?php
//include amp-youtube extention if page contains a YT video
if ($youtube_video_id):
?>
    <script async custom-element="amp-youtube" src="https://cdn.ampproject.org/v0/amp-youtube-0.1.js"></script>
<?php endif;?>

 

Published
Categorized as AMP, Drupal