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

Drupal example source code file (tokenSTARTER.module)

This example Drupal source code file (tokenSTARTER.module) 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

all, array, function, generated, hash, letters, math, mt_rand, number, php, randomly, string, time, type

The tokenSTARTER.module Drupal example source code

<?php

// $Id: tokenSTARTER.module,v 1.1.2.3 2010/03/11 19:28:54 davereid Exp $

/**
 * @file
 * The Token API module.
 *
 * The Token module provides an API for providing tokens to other modules.
 * Tokens are small bits of text that can be placed into larger documents
 * via simple placeholders, like %site-name or [user].
 *
 * @ingroup token
 */

/**
 * Implementation of hook_token_list().
 */
function tokenSTARTER_token_list($type = 'all') {
  $tokens = array();

  if ($type == 'global' || $type == 'all') {
    $tokens['global']['random-sha1']  = t("A randomly generated SHA1 hash.");
    $tokens['global']['site-date-timestamp'] = t('The current timestamp in seconds past January 1, 1970.');
    $tokens['global']['random-num-1'] = t('A randomly generated single-digit number.');
    $tokens['global']['random-num-3'] = t('A randomly generated three-digit number.');
    $tokens['global']['random-num-10'] = t('A randomly generated ten-digit number.');
    $tokens['global']['random-alpha-1'] = t('Randomly generated single-digit letter.');
    $tokens['global']['random-alpha-3'] = t('Randomly generated three-digit letters.');
    $tokens['global']['random-alpha-10'] = t('Randomly generated ten-digit letters.');
  }
  if ($type == 'node' || $type == 'all') {
    // Node tokens here.
  }

  return $tokens;
}

/**
 * Implementation of hook_token_values().
 */
function tokenSTARTER_token_values($type, $object = NULL) {
  $values = array();
  switch ($type) {
    case 'global':
      $values['random-sha1']  = sha1(rand());
      // Create random numbers.
      $values['random-num-1'] = mt_rand(0, 9);
      $values['random-num-3'] = mt_rand(100, 999);
      $values['random-num-10'] = mt_rand(10000, 99999) . mt_rand(10000, 99999);
      // Create random letters.
      $letters = range('a', 'z');
      $values['random-alpha-1'] = $letters[array_rand($letters, 1)];
      shuffle($letters);
      $values['random-alpha-3'] = implode('', array_slice($letters, 0, 3));
      shuffle($letters);
      $values['random-alpha-10'] = implode('', array_slice($letters, 0, 10));
      // Create a UNIX timestamp token.
      $time = time();
      $tz = variable_get('date_default_timezone', 0);
      $values['site-date-timestamp'] = format_date($time, 'custom', 'Y', $tz);

      break;
    case 'node':
      // Node tokens here.
      break;
  }
  return $values;
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal tokenSTARTER.module 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.