? .DS_Store ? index.html ? token.patch ? includes/.DS_Store ? sites/token Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.613 diff -u -F^f -r1.613 common.inc --- includes/common.inc 24 Jan 2007 14:48:35 -0000 1.613 +++ includes/common.inc 28 Jan 2007 22:30:11 -0000 @@ -1832,6 +1832,7 @@ function _drupal_bootstrap_full() { require_once './includes/unicode.inc'; require_once './includes/image.inc'; require_once './includes/form.inc'; + require_once './includes/token.inc'; // Set the Drupal custom error handler. set_error_handler('error_handler'); // Emit the correct charset HTTP header. Index: includes/token.inc =================================================================== RCS file: includes/token.inc diff -N includes/token.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/token.inc 28 Jan 2007 22:30:11 -0000 @@ -0,0 +1,157 @@ +tokens, $prefix, $suffix); + $values = $full->values; + $result = str_replace($tokens, $values, $original); + return $result; +} + +/** + * Return a list of valid substitution tokens and their values for + * the specified type. + * + * @param type + * A flag indicating the class of substitution tokens to use. If an + * object is passed in the second param, 'type' should contain the + * object's type. For example, 'node', 'comment', or 'user'. If no + * type is specified, only 'global' site-wide substitution tokens are + * built. + * @param object + * Optionally, the object to use for building substitution values. + * A node, comment, user, etc. + * @return + * A keyed array containing the substitution tokens and the substition + * values for the passed-in type and object. + */ +function token_get_values($type = 'global', $object = NULL) { + static $tokens; + + include_once('token_node.inc'); + include_once('token_user.inc'); + + if (!isset($tokens)) { + $tokens = array(); + } + + $id = _token_get_id($type, $object); + if (isset($tokens[$type][$id])) { + $tmp_tokens = $tokens[$type][$id]; + } + else { + $tmp_tokens = module_invoke_all('token_values', $type, $object); + $tokens[$type][$id] = $tmp_tokens; + } + + // Special-case global tokens, as we always want to be able to process + // those substitutions. + if (!isset($tokens['global']['default'])) { + $tokens['global']['default'] = module_invoke_all('token_values', 'global'); + } + + $all = array_merge($tokens['global']['default'], $tokens[$type][$id]); + $result->tokens = array_keys($all); + $result->values = array_values($all); + + return $result; +} + +/** + * For a given context, builds a formatted list of tokens and descriptions + * of their replacement values. + * + * @param type + * The token types to display documentation for. Defaults to 'all'. + * @param prefix + * The prefix your module will use when parsing tokens. Defaults to '[' + * @param suffix + * The suffix your module will use when parsing tokens. Defaults to ']' + * @return An HTML table containing the formatting docs. + **/ +function theme_token_help($type = 'all', $prefix = '[', $suffix = ']') { + $full_list = module_invoke_all('token_list', $type); + + $headers = array(t('Token'), t('Replacement value')); + $rows = array(); + foreach ($full_list as $key => $category) { + $rows[] = array(array('data' => drupal_ucfirst($key) . ' ' . t('tokens'), 'class' => 'region', 'colspan' => 2)); + foreach ($category as $token => $description) { + $row = array(); + $row[] = $prefix . $token . $suffix; + $row[] = $description; + $rows[] = $row; + } + } + + $output = theme('table', $headers, $rows, array('class' => 'description')); + return $output; +} + +/** + * A helper function that transforms all the elements of an + * array. Used to change the delimiter style from brackets to + * percent symbols etc. + * + * @param tokens + * The array of tokens keys with no delimiting chacaters + * @param prefix + * Character(s) to prepend to the token key before searching for + * matches. Defaults to an open-bracket. + * @param suffix + * Character(s) to append to the token key before searching for + * matches. Defaults to a close-bracket. + * @return + * The array of token keys, each wrapped in the specified + * delimiter style. + */ +function token_prepare_tokens($tokens = array(), $prefix = '[', $suffix = ']') { + foreach ($tokens as $key => $value) { + $tokens[$key] = $prefix . $value . $suffix; + } + return $tokens; +} + +// Internal utility function used for static caching. Generates +// A unique id given a type and object. +function _token_get_id($type = 'global', $object = NULL) { + if (!isset($object)) { + return "default"; + } + switch ($type) { + case 'node': + return $object->nid; + case 'comment': + return $object->cid; + case 'user': + return $user->uid; + default: + return md5(serialize($object)); + } +} \ No newline at end of file