diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php index 0b5f81d..452f0f4 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php @@ -67,7 +67,7 @@ function testNodeTokenReplacement() { foreach ($tests as $input => $expected) { $output = token_replace($input, array('node' => $node), array('langcode' => $language_interface->langcode)); - $this->assertEqual($output, $expected, t('Sanitized node token %token replaced.', array('%token' => $input))); + $this->assertEqual($output, $expected, format_string('Sanitized node token %token replaced.', array('%token' => $input))); } // Generate and test unsanitized tokens. @@ -79,7 +79,36 @@ function testNodeTokenReplacement() { foreach ($tests as $input => $expected) { $output = token_replace($input, array('node' => $node), array('langcode' => $language_interface->langcode, 'sanitize' => FALSE)); - $this->assertEqual($output, $expected, t('Unsanitized node token %token replaced.', array('%token' => $input))); + $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_NOT_SPECIFIED => 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, $node->langcode, $node->body[$node->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_interface)); + $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[$node->langcode][0]['value']; + + foreach ($tests as $input => $expected) { + $output = token_replace($input, array('node' => $node), array('language' => $language_interface, 'sanitize' => FALSE)); + $this->assertEqual($output, $expected, format_string('Unsanitized node token %token replaced for node without a summary.', array('%token' => $input))); } } } diff --git a/core/modules/node/node.tokens.inc b/core/modules/node/node.tokens.inc index 0bd1000..2d783c9 100644 --- a/core/modules/node/node.tokens.inc +++ b/core/modules/node/node.tokens.inc @@ -136,10 +136,23 @@ function node_tokens($type, $tokens, array $data = array(), array $options = arr case 'body': case 'summary': if ($items = field_get_items('node', $node, 'body', $langcode)) { - $column = ($name == 'body') ? 'value' : 'summary'; $instance = field_info_instance('node', 'body', $node->type); $field_langcode = field_language('node', $node, 'body', $langcode); - $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') { + // Generate an optionally trimmed summary of the body field. + $output = text_summary($output, $instance['settings']['text_processing'] ? $items[0]['format'] : NULL, $instance['display']['teaser']['settings']['trim_length']); + } + } + $replacements[$original] = $output; } break;