Page callback: Generates and prints an RSS feed.
Generates an RSS feed from an array of node IDs, and prints it with an HTTP header, with Content Type set to RSS/XML.
$nids: (optional) An array of node IDs (nid). Defaults to FALSE so empty feeds can be generated with passing an empty array, if no items are to be added to the feed.
$channel: (optional) An associative array containing 'title', 'link', 'description', and other keys, to be parsed by format_rss_channel() and format_xml_elements(). A list of channel elements can be found at the RSS 2.0 Specification. The link should be an absolute URL.
Symfony\Component\HttpFoundation\Response A response object.
function node_feed($nids = FALSE, $channel = array()) {
global $base_url;
$language_content = language(LANGUAGE_TYPE_CONTENT);
$rss_config = config('system.rss');
if ($nids === FALSE) {
$nids = db_select('node', 'n')
->fields('n', array(
'nid',
'created',
))
->condition('n.promote', 1)
->condition('n.status', 1)
->orderBy('n.created', 'DESC')
->range(0, $rss_config
->get('items.limit'))
->addTag('node_access')
->execute()
->fetchCol();
}
$item_length = $rss_config
->get('items.view_mode');
$namespaces = array(
'xmlns:dc' => 'http://purl.org/dc/elements/1.1/',
);
$teaser = $item_length == 'teaser';
// Load all nodes to be rendered.
$nodes = node_load_multiple($nids);
$items = '';
foreach ($nodes as $node) {
$item_text = '';
$node->link = url("node/{$node->nid}", array(
'absolute' => TRUE,
));
$node->rss_namespaces = array();
$node->rss_elements = array(
array(
'key' => 'pubDate',
'value' => gmdate('r', $node->created),
),
array(
'key' => 'dc:creator',
'value' => $node->name,
),
array(
'key' => 'guid',
'value' => $node->nid . ' at ' . $base_url,
'attributes' => array(
'isPermaLink' => 'false',
),
),
);
// The node gets built and modules add to or modify $node->rss_elements
// and $node->rss_namespaces.
$build = node_view($node, 'rss');
unset($build['#theme']);
if (!empty($node->rss_namespaces)) {
$namespaces = array_merge($namespaces, $node->rss_namespaces);
}
if ($item_length != 'title') {
// We render node contents and force links to be last.
$build['links']['#weight'] = 1000;
$item_text .= drupal_render($build);
}
$items .= format_rss_item($node
->label(), $node->link, $item_text, $node->rss_elements);
}
$channel_defaults = array(
'version' => '2.0',
'title' => config('system.site')
->get('name'),
'link' => $base_url,
'description' => $rss_config
->get('channel.description'),
'language' => $language_content->langcode,
);
$channel_extras = array_diff_key($channel, $channel_defaults);
$channel = array_merge($channel_defaults, $channel);
$output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$output .= "<rss version=\"" . $channel["version"] . "\" xml:base=\"" . $base_url . "\" " . new Attribute($namespaces) . ">\n";
$output .= format_rss_channel($channel['title'], $channel['link'], $channel['description'], $items, $channel['language'], $channel_extras);
$output .= "</rss>\n";
return new Response($output, 200, array(
'Content-Type' => 'application/rss+xml; charset=utf-8',
));
}