diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-05-03 10:11:35 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-05-03 10:11:35 +0000 |
commit | a8c30ed91ea00e24f809adc965b05f8292fbc236 (patch) | |
tree | 386db65fe5377df5373bf3e2a2b30bf1112a5fc7 /modules/node | |
parent | 3d951475eaebab990a78d07acdc344eb50239e18 (diff) | |
download | brdo-a8c30ed91ea00e24f809adc965b05f8292fbc236.tar.gz brdo-a8c30ed91ea00e24f809adc965b05f8292fbc236.tar.bz2 |
- Patch #449718 by alienbrain: node_feed() is now using new node building API.
Diffstat (limited to 'modules/node')
-rw-r--r-- | modules/node/node.api.php | 39 | ||||
-rw-r--r-- | modules/node/node.module | 76 | ||||
-rw-r--r-- | modules/node/node.test | 25 | ||||
-rw-r--r-- | modules/node/tests/node_test.module | 27 |
4 files changed, 80 insertions, 87 deletions
diff --git a/modules/node/node.api.php b/modules/node/node.api.php index 65446cad9..39da26ea4 100644 --- a/modules/node/node.api.php +++ b/modules/node/node.api.php @@ -287,28 +287,6 @@ function hook_node_prepare_translation($node) { } /** - * An RSS feed is being generated. - * - * The module can return properties to be added to the RSS item generated for - * this node. This hook should only be used to add XML elements to the RSS - * feed item itself. See comment_node_rss_item() and upload_node_rss_item() - * for examples. - * - * @param $node - * The node the action is being performed on. - * @return - * Extra information to be added to the RSS item. - */ -function hook_node_rss_item($node) { - if ($node->comment != COMMENT_NODE_HIDDEN) { - return array(array('key' => 'comments', 'value' => url('node/' . $node->nid, array('fragment' => 'comments', 'absolute' => TRUE)))); - } - else { - return array(); - } -} - -/** * The node is being displayed as a search result. * * If you want to display extra information with the result, return it. @@ -401,9 +379,20 @@ function hook_node_validate($node, $form) { /** * The node content is being assembled before rendering. * - * The module may add elements $node->content prior to rendering. This hook - * will be called after hook_view(). The structure of $node->content is a renderable - * array as expected by drupal_render(). + * TODO D7 This needs work to clearly explain the different build modes. + * + * The module may add elements to $node->content prior to rendering. This hook + * will be called after hook_view(). The structure of $node->content is a + * renderable array as expected by drupal_render(). + * + * When $node->build_mode is NODE_BUILD_RSS modules can also add extra RSS + * elements and namespaces to $node->rss_elements and $node->rss_namespaces + * respectively for the RSS item generated for this node. For details on how + * this is used @see node_feed() + * + * @see taxonomy_node_view() + * @see upload_node_view() + * @see comment_node_view() * * @param $node * The node the action is being performed on. diff --git a/modules/node/node.module b/modules/node/node.module index 92a088871..b557a56cb 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -1950,70 +1950,38 @@ function node_feed($nids = FALSE, $channel = array()) { $item_length = variable_get('feed_item_length', 'teaser'); $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 $item) { - $item->build_mode = NODE_BUILD_RSS; - $item->link = url("node/$item->nid", array('absolute' => TRUE)); - - if ($item_length != 'title') { - $teaser = ($item_length == 'teaser'); - - // Filter and prepare node teaser - if (node_hook($item, 'view')) { - $item = node_invoke($item, 'view', $teaser, FALSE); - } - else { - $item = node_prepare($item, $teaser); - } - - // Allow modules to change $node->content before the node is rendered. - module_invoke_all('node_view', $item, $teaser); - - // Set the proper node property, then unset unused $node property so that a - // bad theme can not open a security hole. - $content = drupal_render($item->content); - if ($teaser) { - $item->teaser = $content; - unset($item->body); - } - else { - $item->body = $content; - unset($item->teaser); - } + foreach ($nodes as $node) { + $item_text = ''; + + $node->build_mode = NODE_BUILD_RSS; + $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')) + ); - // Allow modules to modify the fully-built node. - module_invoke_all('node_alter', $item, $teaser, FALSE); - } + // The node gets built and modules add to or modify $node->rss_elements + // and $node->rss_namespaces. + $node = node_build_content($node, $teaser); - // Allow modules to add additional item fields and/or modify $item - $extra = module_invoke_all('node_rss_item', $item); - $extra = array_merge($extra, array(array('key' => 'pubDate', 'value' => gmdate('r', $item->created)), array('key' => 'dc:creator', 'value' => $item->name), array('key' => 'guid', 'value' => $item->nid . ' at ' . $base_url, 'attributes' => array('isPermaLink' => 'false')))); - foreach ($extra as $element) { - if (isset($element['namespace'])) { - $namespaces = array_merge($namespaces, $element['namespace']); - } + if (!empty($node->rss_namespaces)) { + $namespaces = array_merge($namespaces, $node->rss_namespaces); } - // Prepare the item description - switch ($item_length) { - case 'fulltext': - $item_text = $item->body; - break; - case 'teaser': - $item_text = $item->teaser; - if (!empty($item->readmore)) { - $item_text .= '<p>' . l(t('read more'), 'node/' . $item->nid, array('absolute' => TRUE, 'attributes' => array('target' => '_blank'))) . '</p>'; - } - break; - case 'title': - $item_text = ''; - break; + if ($item_length != 'title' && !empty($node->content)) { + // We render node contents and force links to be last. + $links = drupal_render($node->content['links']); + $item_text .= drupal_render($node->content) . $links; } - $items .= format_rss_item($item->title, $item->link, $item_text, $extra); + $items .= format_rss_item($node->title, $node->link, $item_text, $node->rss_elements); } $channel_defaults = array( diff --git a/modules/node/node.test b/modules/node/node.test index 022b0cf78..cf2cb4802 100644 --- a/modules/node/node.test +++ b/modules/node/node.test @@ -658,10 +658,29 @@ class NodeRSSContentTestCase extends DrupalWebTestCase { // Create a node. $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1)); - $test_text = t('Extra test data added to node !nid.', array('!nid' => $node->nid)); - $this->drupalGet('rss.xml'); - $this->assertText($test_text, t('Extra node content appears in RSS feed.')); + + // Check that content added in NODE_BUILD_RSS appear in RSS feed. + $rss_only_content = t('Extra data that should appear only in the RSS feed for node !nid.', array('!nid' => $node->nid)); + $this->assertText($rss_only_content, t('Node content designated for RSS appear in RSS feed.')); + + // Check that content added in build modes other than NODE_BUILD_RSS doesn't + // appear in RSS feed. + $non_rss_content = t('Extra data that should appear everywhere except the RSS feed for node !nid.', array('!nid' => $node->nid)); + $this->assertNoText($non_rss_content, t('Node content not designed for RSS doesn\'t appear in RSS feed.')); + + // Check that extra RSS elements and namespaces are added to RSS feed. + $test_element = array( + 'key' => 'testElement', + 'value' => t('Value of testElement RSS element for node !nid.', array('!nid' => $node->nid)), + ); + $test_ns = 'xmlns:drupaltest="http://example.com/test-namespace"'; + $this->assertRaw(format_xml_elements(array($test_element)), t('Extra RSS elements appear in RSS feed.')); + $this->assertRaw($test_ns, t('Extra namespaces appear in RSS feed.')); + + // Check that content added in NODE_BUILD_RSS doesn't appear when viewing node. + $this->drupalGet("node/$node->nid"); + $this->assertNoText($rss_only_content, t('Node content designed for RSS doesn\'t appear when viewing node.')); } } diff --git a/modules/node/tests/node_test.module b/modules/node/tests/node_test.module index d42172b6b..1ad3227ff 100644 --- a/modules/node/tests/node_test.module +++ b/modules/node/tests/node_test.module @@ -8,11 +8,28 @@ */ /** - * When the module is enabled, text will be added to all nodes in all build modes. + * Implementation of hook_node_view(). */ function node_test_node_view($node, $teaser) { - $node->content['node_test_extra_field'] = array( - '#markup' => '<p>' . t('Extra test data added to node !nid.', array('!nid' => $node->nid)) . '</p>', - '#weight' => 10, - ); + if ($node->build_mode == NODE_BUILD_RSS) { + // Add RSS elements and namespaces when building the RSS feed. + $node->rss_elements[] = array( + 'key' => 'testElement', + 'value' => t('Value of testElement RSS element for node !nid.', array('!nid' => $node->nid)), + ); + $node->rss_namespaces['xmlns:drupaltest'] = 'http://example.com/test-namespace'; + + // Add content that should be displayed only in the RSS feed. + $node->content['extra_feed_content'] = array( + '#markup' => '<p>' . t('Extra data that should appear only in the RSS feed for node !nid.', array('!nid' => $node->nid)) . '</p>', + '#weight' => 10, + ); + } + + if ($node->build_mode != NODE_BUILD_RSS) { + // Add content that should NOT be displayed in the RSS feed. + $node->content['extra_non_feed_content'] = array( + '#markup' => '<p>' . t('Extra data that should appear everywhere except the RSS feed for node !nid.', array('!nid' => $node->nid)) . '</p>', + ); + } } |