commit 9b033fc32bdf349ab34d5eb66c70046f17e5b995 Author: Joel Pittet Date: Sun Apr 7 17:54:23 2013 -0700 aggregator_page_rss and block_item conversion diff --git a/core/includes/common.inc b/core/includes/common.inc index fbacae6..40d4925 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -1594,46 +1594,6 @@ function filter_xss_bad_protocol($string, $decode = TRUE) { */ /** - * Formats an RSS channel. - * - * Arbitrary elements may be added using the $args associative array. - */ -function format_rss_channel($title, $link, $description, $items, $langcode = NULL, $args = array()) { - $langcode = $langcode ? $langcode : language(LANGUAGE_TYPE_CONTENT)->langcode; - - $output = "\n"; - $output .= ' ' . check_plain($title) . "\n"; - $output .= ' ' . check_url($link) . "\n"; - - // The RSS 2.0 "spec" doesn't indicate HTML can be used in the description. - // We strip all HTML tags, but need to prevent double encoding from properly - // escaped source data (such as & becoming &). - $output .= ' ' . check_plain(decode_entities(strip_tags($description))) . "\n"; - $output .= ' ' . check_plain($langcode) . "\n"; - $output .= format_xml_elements($args); - $output .= $items; - $output .= "\n"; - - return $output; -} - -/** - * Formats a single RSS item. - * - * Arbitrary elements may be added using the $args associative array. - */ -function format_rss_item($title, $link, $description, $args = array()) { - $output = "\n"; - $output .= ' ' . check_plain($title) . "\n"; - $output .= ' ' . check_url($link) . "\n"; - $output .= ' ' . check_plain($description) . "\n"; - $output .= format_xml_elements($args); - $output .= "\n"; - - return $output; -} - -/** * Formats XML elements. * * @param $array diff --git a/core/modules/aggregator/aggregator.module b/core/modules/aggregator/aggregator.module index 2aa7a87..ccf9062 100644 --- a/core/modules/aggregator/aggregator.module +++ b/core/modules/aggregator/aggregator.module @@ -63,6 +63,7 @@ function aggregator_theme() { ), 'aggregator_block_item' => array( 'variables' => array('item' => NULL, 'feed' => 0), + 'template' => 'aggregator-block-item', ), 'aggregator_summary_items' => array( 'variables' => array('summary_items' => NULL, 'source' => NULL), @@ -72,6 +73,7 @@ function aggregator_theme() { 'aggregator_summary_item' => array( 'variables' => array('aggregator_item' => NULL, 'view_mode' => NULL), 'file' => 'aggregator.pages.inc', + 'template' => 'aggregator-summary-item', ), 'aggregator_item' => array( 'variables' => array('aggregator_item' => NULL, 'view_mode' => NULL), @@ -81,10 +83,12 @@ function aggregator_theme() { 'aggregator_page_opml' => array( 'variables' => array('feeds' => NULL), 'file' => 'aggregator.pages.inc', + 'template' => 'aggregator-page-opml', ), 'aggregator_page_rss' => array( 'variables' => array('feeds' => NULL, 'category' => NULL), 'file' => 'aggregator.pages.inc', + 'template' => 'aggregator-page-rss', ), ); } @@ -523,18 +527,19 @@ function aggregator_category_load($cid) { } /** - * Returns HTML for an individual feed item for display in the block. + * Processes variables for an individual feed item for display in the block. + * + * Default template: aggregator-item.tpl.php * * @param $variables * An associative array containing: * - item: The item to be displayed. * - feed: Not used. - * - * @ingroup themeable */ -function theme_aggregator_block_item($variables) { +function template_preprocess_aggregator_block_item(&$variables) { // Display the external link to the item. - return '' . check_plain($variables['item']->title) . "\n"; + $variables['url'] = check_url($variables['item']->link); + $variables['title'] = check_plain($variables['item']->title); } /** diff --git a/core/modules/aggregator/aggregator.pages.inc b/core/modules/aggregator/aggregator.pages.inc index c9fc490..181095f 100644 --- a/core/modules/aggregator/aggregator.pages.inc +++ b/core/modules/aggregator/aggregator.pages.inc @@ -415,35 +415,37 @@ function aggregator_page_rss() { } $feeds = $result->fetchAll(); - return theme('aggregator_page_rss', array('feeds' => $feeds, 'category' => $category)); + return array( + '#theme' => 'aggregator_page_rss', + '#feeds' => $feeds, + '#category' => $category, + ); } /** - * Prints the RSS page for a feed. + * Prepares variables for a RSS feed page template. * - * @param $variables + * Default template: aggregator-page-rss.html.twig. + * + * @param array $variables * An associative array containing: * - feeds: An array of the feeds to theme. * - category: A common category, if any, for all the feeds. - * - * @return void - * - * @ingroup themeable */ -function theme_aggregator_page_rss($variables) { +function template_preprocess_aggregator_page_rss(&$variables) { $feeds = $variables['feeds']; $category = $variables['category']; drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8'); - $items = ''; - $feed_length = config('system.rss')->get('items.view_mode'); - foreach ($feeds as $feed) { + foreach ($feeds as &$feed) { + $feed->show_more = FALSE; + switch ($feed_length) { case 'teaser': $summary = text_summary($feed->description, NULL, config('aggregator.settings')->get('items.teaser_length')); if ($summary != $feed->description) { - $summary .= '

' . t('read more') . "

\n"; + $feed->show_more = TRUE; } $feed->description = $summary; break; @@ -451,19 +453,39 @@ function theme_aggregator_page_rss($variables) { $feed->description = ''; break; } - $items .= format_rss_item($feed->ftitle . ': ' . $feed->title, $feed->link, $feed->description, array('pubDate' => date('r', $feed->timestamp))); + $feed->title = check_plain($feed->ftitle . ': ' . $feed->title); + $feed->link = check_url($feed->link); + $feed->description = check_plain($feed->description); + if (empty($feed->args) || !is_array($feed->args)) { + $feed->args = array(); + } + $feed->args += array('pubDate' => date('r', $feed->timestamp)); + $feed->args = format_xml_elements($feed->args); + } $site_name = config('system.site')->get('name'); $url = url((isset($category) ? 'aggregator/categories/' . $category->cid : 'aggregator'), array('absolute' => TRUE)); $description = isset($category) ? t('@site_name - aggregated feeds in category @title', array('@site_name' => $site_name, '@title' => $category->title)) : t('@site_name - aggregated feeds', array('@site_name' => $site_name)); - $output = "\n"; - $output .= "\n"; - $output .= format_rss_channel(t('@site_name aggregator', array('@site_name' => $site_name)), $url, $description, $items); - $output .= "\n"; - - print $output; + $variables['feed_length'] = config('system.rss')->get('items.view_mode'); + $variables['title'] = check_plain(t('@site_name aggregator', array('@site_name' => $site_name))); + $variables['link'] = check_url($url); + // The RSS 2.0 "spec" doesn't indicate HTML can be used in the description. + // We strip all HTML tags, but need to prevent double encoding from properly + // escaped source data (such as & becoming &). + $variables['description'] = check_plain(decode_entities(strip_tags($description))); + $variables['items'] = $feeds; + + $langcode = (isset($category['langcode']) ? $category['langcode'] : language(LANGUAGE_TYPE_CONTENT)->langcode); + $variables['langcode'] = check_plain($langcode); + + // Pass along any channel args to be rendered after the language code. + if (isset($category['args']) && is_array($category['args'])) { + $variables['args'] = format_xml_elements($category['args']); + } else { + $variables['args'] = ''; + } } /** @@ -491,13 +513,13 @@ function aggregator_page_opml($cid = NULL) { } /** - * Prints the OPML page for the feed. + * Prepares variables for an OPML feed page template. + * + * Default template: aggregator-page-opml.html.twig. * * @param array $variables * An associative array containing: * - feeds: An array of the feeds to theme. - * - * @ingroup themeable */ function template_preprocess_aggregator_page_opml(&$variables) { $feeds = $variables['feeds']; diff --git a/core/modules/aggregator/templates/aggregator-block-item.html.twig b/core/modules/aggregator/templates/aggregator-block-item.html.twig new file mode 100644 index 0000000..10bc490 --- /dev/null +++ b/core/modules/aggregator/templates/aggregator-block-item.html.twig @@ -0,0 +1,16 @@ +{# +/** + * @file + * Default theme implementation for feed item for display in the block. + * + * Available variables: + * - url: URL to the feed item. + * - title: Title of the feed item. + * + * @see template_preprocess() + * @see template_preprocess_aggregator_block_item() + * + * @ingroup themeable + */ +#} +{{ title }} diff --git a/core/modules/aggregator/templates/aggregator-page-opml.html.twig b/core/modules/aggregator/templates/aggregator-page-opml.html.twig index b72d0d7..3a9f28d 100644 --- a/core/modules/aggregator/templates/aggregator-page-opml.html.twig +++ b/core/modules/aggregator/templates/aggregator-page-opml.html.twig @@ -4,11 +4,11 @@ * Default theme implementation to present @todo. * * Available variables: - * - title: @todo - * - date: @todo - * - feeds: @todo - * - feed.title: @todo - * - feed.url: @todo + * - title: @todo. + * - date: @todo. + * - feeds: @todo. + * - feed.title: @todo. + * - feed.url: @todo. * * @see template_preprocess() * @see template_preprocess_aggregator_page_opml() @@ -24,7 +24,7 @@ {% for feed in feeds %} - + {% endfor %} diff --git a/core/modules/aggregator/templates/aggregator-page-rss.html.twig b/core/modules/aggregator/templates/aggregator-page-rss.html.twig new file mode 100644 index 0000000..aefe283 --- /dev/null +++ b/core/modules/aggregator/templates/aggregator-page-rss.html.twig @@ -0,0 +1,41 @@ +{# +/** + * @file + * Default theme implementation to present @todo. + * + * Available variables: + * - title: @todo + * - date: @todo + * - feeds: @todo + * - feed.title: @todo + * - feed.url: @todo + * + * @see template_preprocess() + * @see template_preprocess_aggregator_page_opml() + * + * @ingroup themeable + */ +#} + + + + {{ title }} + {{ link }} + {{ description }} + {{ langcode }} + {{ args }} + {% for item in items %} + + {{ item.title }} + {{ item.link }} + + {{- item.description }} + {% if item.show_more %} +

{{ 'read more' | t }}

+ {% endif %} +
+ {{ item.args }} +
+ {% endfor %} +
+
diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 36dcfe0..8d3e897 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -2098,7 +2098,7 @@ function node_feed($nids = FALSE, $channel = array()) { // Load all nodes to be rendered. $nodes = node_load_multiple($nids); - $items = ''; + $items = array(); foreach ($nodes as $node) { $item_text = ''; @@ -2124,8 +2124,13 @@ function node_feed($nids = FALSE, $channel = array()) { $build['links']['#weight'] = 1000; $item_text .= drupal_render($build); } + $item = new stdClass(); + $item->title = $node->label(); + $item->link = $node->link; + $feed->description = $item_text; + $feed->args = $node->rss_elements; - $items .= format_rss_item($node->label(), $node->link, $item_text, $node->rss_elements); + $items[] = $item; } $channel_defaults = array( @@ -2133,17 +2138,16 @@ function node_feed($nids = FALSE, $channel = array()) { 'title' => config('system.site')->get('name'), 'link' => $base_url, 'description' => $rss_config->get('channel.description'), - 'language' => $language_content->langcode + 'langcode' => $language_content->langcode, ); $channel_extras = array_diff_key($channel, $channel_defaults); $channel = array_merge($channel_defaults, $channel); - $output = "\n"; - $output .= "\n"; - $output .= format_rss_channel($channel['title'], $channel['link'], $channel['description'], $items, $channel['language'], $channel_extras); - $output .= "\n"; - - return new Response($output, 200, array('Content-Type' => 'application/rss+xml; charset=utf-8')); + return array( + '#theme' => 'aggregator_page_rss', + '#category' => (object) $channel, + '#feeds' => $items, + ); } /**