custom token

Public

Creation of custom token/placeholder for Drupal 7. copied from

Get raw version
php
  1. <?php
  2. /**
  3.  * @file
  4.  * Provides a special token to use with pathauto.
  5.  *
  6.  * @see http://drupal.org/node/1308488
  7.  */
  8.  
  9. /**
  10.  * Provide information about my custom placeholder/token.
  11.  *
  12.  * @see http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_token_info/7
  13.  * @return array
  14.  * An associative array of available tokens and token types..
  15.  */
  16. function MODULE_NAME_token_info() {
  17. $info['tokens']['node']['pathauto'] = array(
  18. 'name' => t('Pathauto'),
  19. 'description' => t('Title ready for use with pathauto.'),
  20. );
  21. return $info;
  22. }
  23.  
  24. /**
  25.  * Provide replacement values for placeholder tokens.
  26.  *
  27.  * @see http://api-drupal.org/api/drupal/modules--system--system.api.php/function/hook_tokens/7
  28.  * @param string $type
  29.  * The machine-readable name of the type (group) of token being replaced, such as
  30.  * 'node', 'user', or another type of defined by a hook_token_info() implementation.
  31.  * @param array $tokens
  32.  * An array of tokens to be replaced. The keys are the machine-readable token names,
  33.  * and the values are the raw [type:token] that appeared in the original text.
  34.  * @param array $data (optional)
  35.  * An associative array of data objects to be used when generating replacement values
  36.  * as supplied in the $data parameter to token_replace().
  37.  * @param array $options (optional)
  38.  * An associative array of options for token replacement; see token_replace for
  39.  * possible values.
  40.  *@return array
  41.  * An associative of replacement values, keyed by the raw [type:token] strings from
  42.  * from the original text.
  43.  */
  44. function MODULE_NAME_tokens($type, $tokens, array $data = array(), array $options = array()) {
  45. $replacements = array();
  46. $sanitize = !empty($options['sanitize']);
  47.  
  48. if ($type == 'node' && !empty($data['node'])) {
  49. $node = $data['node'];
  50.  
  51. foreach ($tokens as $name => $original) {
  52. switch($name) {
  53. case 'pathauto':
  54. $replacements[$original] = str_replace(
  55. array('&amp;', '&'),
  56. 'and',
  57. '$sanitize ? filter_xss($node->title) : $node->title
  58. );
  59. break;
  60. }
  61. }
  62. return $replacements;
  63.  
  64. }
  65. ?>