diff --git a/tests/token_test.module b/tests/token_test.module index 5c8bab0..82c9db2 100644 --- a/tests/token_test.module +++ b/tests/token_test.module @@ -14,6 +14,16 @@ function token_test_exit() { } /** + * Implements hook_entity_presave(). + */ +function token_test_entity_presave($entity, $entity_type) { + if ($string = variable_get('token_entity_presave_string_input')) { + $string = token_replace($string, array($entity_type => $entity)); + variable_set('token_entity_presave_string_output', $string); + } +} + +/** * Implements hook_date_format_types(). * * @todo Remove when http://drupal.org/node/1173706 is fixed. diff --git a/token.test b/token.test index 2f943c6..90dba7d 100644 --- a/token.test +++ b/token.test @@ -698,6 +698,20 @@ class TokenEntityTestCase extends TokenTestHelper { taxonomy_term_save($term); return $term; } + + function testEntityOriginal() { + variable_set('token_entity_presave_string_input', '[node:original] [node:original:nid] is now [node:title]'); + $node = $this->drupalCreateNode(array('title' => 'Original title')); + + $this->refreshVariables(); + $this->assertEqual(variable_get('token_entity_presave_string_output'), '[node:original] [node:original:nid] is now Original title'); + + $node->title = 'New title'; + node_save($node); + + $this->refreshVariables(); + $this->assertEqual(variable_get('token_entity_presave_string_output'), 'Original title 1 is now New title'); + } } /** diff --git a/token.tokens.inc b/token.tokens.inc index 13df37f..2769edd 100644 --- a/token.tokens.inc +++ b/token.tokens.inc @@ -62,6 +62,16 @@ function token_token_info_alter(&$info) { 'type' => 'url', ); } + + // Add [entity:original] tokens if they do not already exist. + if (!isset($info['tokens'][$token_type]['original'])) { + $info['tokens'][$token_type]['original'] = array( + 'name' => t('Original @entity', array('@entity' => drupal_strtolower($entity_info['label']))), + 'description' => t('The original @entity if tokens are being replaced when the entity is being saved.', array('@entity' => drupal_strtolower($entity_info['label']))), + 'module' => 'token', + 'type' => $token_type, + ); + } } // Add support for custom date formats. @@ -762,14 +772,26 @@ function token_tokens($type, $tokens, array $data = array(), array $options = ar $replacements[$original] = url($uri['path'], $uri['options']); } break; + + case 'original': + if (_token_module($type, 'original') == 'token' && !empty($entity->original)) { + $label = entity_label($entity_type, $entity->original); + $replacements[$original] = $sanitize ? check_plain($label) : $label; + } + break; } } - // Chained token relationships. + // [entity:url:*] chained tokens. if (($url_tokens = token_find_with_prefix($tokens, 'url')) && _token_module($type, 'url') == 'token') { $replacements += token_generate('url', $url_tokens, entity_uri($entity_type, $entity), $options); } + // [entity:original:*] chained tokens. + if (($original_tokens = token_find_with_prefix($tokens, 'original')) && _token_module($type, 'original') == 'token' && !empty($entity->original)) { + $replacements += token_generate($type, $original_tokens, array($type => $entity->original), $options); + } + // Pass through to an generic 'entity' token type generation. $entity_data = array( 'entity_type' => $entity_type,