diff options
author | David Rothstein <drothstein@gmail.com> | 2014-05-05 23:54:18 -0400 |
---|---|---|
committer | David Rothstein <drothstein@gmail.com> | 2014-05-05 23:54:18 -0400 |
commit | e928b8537bc2987ab4c1a0b5f944eed1957b253c (patch) | |
tree | ecbfcdc03b2febc8155a7a2b0ad4b02ef904d34c | |
parent | 20c04c587beb78094cdc1302f9c226af8931747c (diff) | |
download | brdo-e928b8537bc2987ab4c1a0b5f944eed1957b253c.tar.gz brdo-e928b8537bc2987ab4c1a0b5f944eed1957b253c.tar.bz2 |
Issue #1300920 by salvis, DamienMcKenna, mkadin, idflood, pjcdawkins, lyricnz, greggles, Niklas Fiekas, gumanist | Dave Reid: The [node:summary] token does not output anything for body fields without a manual summary.
-rw-r--r-- | CHANGELOG.txt | 2 | ||||
-rw-r--r-- | modules/node/node.test | 29 | ||||
-rw-r--r-- | modules/node/node.tokens.inc | 23 |
3 files changed, 52 insertions, 2 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt index eb47606c9..79ac7cb4f 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,6 +1,8 @@ Drupal 7.28, xxxx-xx-xx (development version) ----------------------- +- Fixed the behavior of the token system's "[node:summary]" token when the body + field does not have a manual summary. - Changed the behavior of db_query_temporary() so that it works on SELECT queries even when they have leading comments/whitespace. A side effect of this fix is that db_query_temporary() will now fail with an error if it is diff --git a/modules/node/node.test b/modules/node/node.test index 93d58b530..0777e1137 100644 --- a/modules/node/node.test +++ b/modules/node/node.test @@ -2444,6 +2444,35 @@ class NodeTokenReplaceTestCase extends DrupalWebTestCase { $output = token_replace($input, array('node' => $node), array('language' => $language, 'sanitize' => FALSE)); $this->assertEqual($output, $expected, format_string('Unsanitized node token %token replaced.', array('%token' => $input))); } + + // Repeat for a node without a summary. + $settings['body'] = array(LANGUAGE_NONE => array(array('value' => $this->randomName(32), 'summary' => ''))); + $node = $this->drupalCreateNode($settings); + + // Load node (without summary) so that the body and summary fields are + // structured properly. + $node = node_load($node->nid); + $instance = field_info_instance('node', 'body', $node->type); + + // Generate and test sanitized token - use full body as expected value. + $tests = array(); + $tests['[node:summary]'] = _text_sanitize($instance, $langcode, $node->body[$langcode][0], 'value'); + + // Test to make sure that we generated something for each token. + $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated for node without a summary.'); + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array('node' => $node), array('language' => $language)); + $this->assertEqual($output, $expected, format_string('Sanitized node token %token replaced for node without a summary.', array('%token' => $input))); + } + + // Generate and test unsanitized tokens. + $tests['[node:summary]'] = $node->body[$langcode][0]['value']; + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array('node' => $node), array('language' => $language, 'sanitize' => FALSE)); + $this->assertEqual($output, $expected, format_string('Unsanitized node token %token replaced for node without a summary.', array('%token' => $input))); + } } } diff --git a/modules/node/node.tokens.inc b/modules/node/node.tokens.inc index e43db5e7d..e63c751d6 100644 --- a/modules/node/node.tokens.inc +++ b/modules/node/node.tokens.inc @@ -136,10 +136,29 @@ function node_tokens($type, $tokens, array $data = array(), array $options = arr case 'body': case 'summary': if ($items = field_get_items('node', $node, 'body', $language_code)) { - $column = ($name == 'body') ? 'value' : 'summary'; $instance = field_info_instance('node', 'body', $node->type); $field_langcode = field_language('node', $node, 'body', $language_code); - $replacements[$original] = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], $column) : $items[0][$column]; + // If the summary was requested and is not empty, use it. + if ($name == 'summary' && !empty($items[0]['summary'])) { + $output = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'summary') : $items[0]['summary']; + } + // Attempt to provide a suitable version of the 'body' field. + else { + $output = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'value') : $items[0]['value']; + // A summary was requested. + if ($name == 'summary') { + if (isset($instance['display']['teaser']['settings']['trim_length'])) { + $trim_length = $instance['display']['teaser']['settings']['trim_length']; + } + else { + // Use default value. + $trim_length = NULL; + } + // Generate an optionally trimmed summary of the body field. + $output = text_summary($output, $instance['settings']['text_processing'] ? $items[0]['format'] : NULL, $trim_length); + } + } + $replacements[$original] = $output; } break; |