home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (token.install)

This example Drupal source code file (token.install) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, cache, foreach, function, modules, new_token, old_token, php, regex, return, text, tokens, trailing, updates

The token.install Drupal example source code

<?php
// $Id: token.install,v 1.15 2010/12/08 16:25:09 davereid Exp $

/**
 * Implements hook_requirements().
 */
function token_requirements($phase = 'runtime') {
  $requirements = array();
  $t = get_t();

  // Check for duplicate tokens.
  if ($phase == 'runtime') {
    if ($duplicate_tokens = token_find_duplicate_tokens()) {
      foreach ($duplicate_tokens as $token => $modules) {
        $duplicate_tokens[$token] = $t('@token (defined by modules: @modules)', array('@token' => $token, '@modules' => implode(', ', $modules)));
      }
      $requirements['token_duplicates'] = array(
        'title' => $t('Duplicate tokens'),
        'value' => $t('The following tokens are defined by multiple modules and may cause problems when performing token replacement.'),
        'severity' => REQUIREMENT_WARNING,
        'description' => theme('item_list', array('items' => $duplicate_tokens)),
      );
    }
  }

  return $requirements;
}

/**
 * Implements hook_schema().
 */
function token_schema() {
  $schema['cache_token'] = drupal_get_schema_unprocessed('system', 'cache');
  $schema['cache_token']['description'] = 'Cache table for token information.';
  return $schema;
}

/**
 * Add the cache_token table.
 */
function token_update_7000() {
  $schema['cache_token'] = drupal_get_schema_unprocessed('system', 'cache');
  $schema['cache_token']['description'] = 'Cache table for token information.';
  db_create_table('cache_token', $schema['cache_token']);
}

/**
 * Deprecate and disable the token_actions module.
 */
function token_update_7001() {
  module_disable(array('token_actions'));
  return 'The Token actions module has been deprecated by the updated system module actions that support tokens.';
}

/**
 * Migrate old token_actions module actions to system actions.
 */
//function token_update_700X() {
//  $actions = db_query("SELECT aid, type, callback, parameters, label FROM {actions} WHERE type = 'system' AND callback IN ('token_actions_message_action', 'token_actions_send_email_action', 'token_actions_goto_action')")->fetchAll();
//  foreach ($actions as $action) {
//    $action->parameters = unserialize($action->parameters);
//    foreach ($action->parameters as $key => $value) {
//      if (is_string($value)) {
//        $action->parameters[$key] = token_update_token_text($value);
//      }
//    }
//    $action->callback = str_replace('token_actions_', '', $action->callback);
//    actions_save($action->callback, $action->type, $action->parameters, $action->label, $action->aid);
//  }
//  return 'Token actions module actions migrated to system module actions. You may still want to verify that the actions were upgraded correctly.';
//}

/**
 * Build a list of Drupal 6 tokens and their Drupal 7 token names.
 */
function _token_upgrade_token_list() {
  $tokens = array(
    // Global tokens
    'user-name' => 'current-user:name',
    'user-id' => 'current-user:id',
    'user-mail' => 'current-user:mail',
    'site-url' => 'site:url',
    'site-name' => 'site:name',
    'site-slogan' => 'site:slogan',
    'site-mission' => 'site:mission',
    'site-mail' => 'site:mail',
    'site-date' => 'date:short',
    //'site-date-' => '', // Date tokens expanded below
    'current-page-path' => 'current-page:path',
    'current-page-url' => 'current-page:url',
    'page-number' => 'current-page:page-number',

    // Comment tokens
    'comment-cid' => 'comment:cid',
    'comment-nid' => 'comment:node:nid',
    'comment-title' => 'comment:title',
    'comment-body' => 'comment:body',
    'comment-author-name' => 'comment:author:name',
    'comment-author-mail' => 'comment:author:mail',
    //'comment-body-format' => '',
    //'comment-' => '', // Date tokens expanded below
    'comment-node-title' => 'comment:node',

    // Node tokens
    'nid' => 'node:nid',
    'type' => 'node:type',
    'type-name' => 'node:type-name',
    'language' => 'node:language',
    'title' => 'node:title',
    'author-uid' => 'node:author:uid',
    'author-name' => 'node:author:name',
    'author-mail' => 'node:author:mail',
    'node_comment_count' => 'node:comment-count',
    'unread_comment_count' => 'node:comment-count-new',
    'log' => 'node:log',
    //'' => '', // Date tokens expanded below
    //'mod-' => '', // Date tokens expanded below
    'menupath' => 'node:menu-link:parent:path][node:menu-link',
    'menu' => 'node:menu-link:menu-name',
    'menu-link-title' => 'node:menu-link',
    'menu-link-mlid' => 'node:menu-link:mlid',
    'menu-link-plid' => 'node:menu-link:parent:mlid',
    //'term' => 'node:term',
    //'term-id' => 'node:term:tid',
    //'vocab' => 'node:term:vocabulary',
    //'vocab-id' => 'node:term:vocabulary:vid',

    // Book tokens
    //'book' => 'node:book',
    //'book_id' => 'node:book:bid',
    //'bookpath' => 'node:book:path',

    // Taxonomy tokens
    'tid' => 'term:tid',
    'cat' => 'term:name',
    'cat-description' => 'term:description',
    'vid' => 'term:vocabulary:vid',
    'vocab' => 'term:vocabulary',
    'vocab-description' => 'term:vocabulary:description',

    // User tokens
    'user' => 'user:name',
    'uid' => 'user:uid',
    'mail' => 'user:mail',
    'reg-date' => 'user:created',
    'reg-since' => 'user:created:since',
    //'user-created' => '', // Date tokens expanded below
    'log-date' => 'user:last-login',
    'log-since' => 'user:last-login:since',
    //'user-last-login' => '', // Date tokens expanded below
    //'date-in-tz' => '',
    'account-url' => 'user:url',
    'account-edit' => 'user:edit-url',
  );

  // Account for date tokens which need to be expanded.
  $tokens += _token_upgrade_token_date_list('site-', 'site:date');
  $tokens += _token_upgrade_token_date_list('', 'node:created');
  $tokens += _token_upgrade_token_date_list('mod-', 'node:changed');
  //$tokens += _token_upgrade_token_date_list('node-revision-', 'node:changed');
  $tokens += _token_upgrade_token_date_list('comment-', 'comment:created');
  $tokens += _token_upgrade_token_date_list('user-register-', 'user:created');
  $tokens += _token_upgrade_token_date_list('user-last-login-', 'user:last-login');

  return $tokens;
}

/**
 * Build a list of Drupal 6 date tokens and their Drupal 7 token names.
 */
function _token_upgrade_token_date_list($old_token, $new_token) {
  $tokens = array();
  $formats = array(
    'yyyy' => 'Y',
    'yy' => 'y',
    'month' => 'F',
    'mon' => 'M',
    'mm' => 'm',
    'm' => 'n',
    'ww' => 'W',
    'date' => 'N',
    'day' => 'l',
    'ddd' => 'D',
    'dd' => 'd',
    'd' => 'j',
  );
  foreach ($formats as $token_format => $date_format) {
    $tokens[$old_token . $token_format] = "$new_token:custom:$date_format";
  }
  $tokens[$old_token . 'raw'] = "$new_token:raw";
  $tokens[$old_token . 'since'] = "$new_token:since";
  return $tokens;
}

/**
 * Update a string containing Drupal 6 style tokens to Drupal 7 style tokens.
 *
 * @param $text
 *   A string containing tokens.
 * @param $updates
 *   An optional array of Drupal 7 tokens keyed by their Drupal 6 token name.
 *   The default tokens will be merged into this array. Note neither the old
 *   or new token names should include the surrounding bracket ([ and ])
 *   characters.
 * @return
 *   A string with the tokens upgraded
 *
 * @see _token_upgrade_token_list();
 */
function token_update_token_text($text, $updates = array(), $leading = '[', $trailing = ']') {
  $updates += _token_upgrade_token_list();
  $regex = '/' . preg_quote($leading, '/') . '([^\s]*)' . preg_quote($trailing, '/') . ']/';
  preg_match_all($regex, $text, $matches);

  foreach ($matches[1] as $index => $old_token) {
    if (isset($updates[$old_token])) {
      $new_token = $updates[$old_token];
      $text = str_replace("{$leading}{$old_token}{$trailing}", "[$new_token]", $text);
      // Also replace any tokens that have a -raw suffix.
      $text = str_replace("{$leading}{$old_token}-raw{$trailing}", "[$new_token]", $text);
    }
  }

  return $text;
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal token.install source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.