Drupal で、記事にランダムな URL をつけるモジュールを作成しました。 http://example.com/o8Uyswfi みたいな、英数文字のランダム文字列URLです。YouTube でも使われている形式です。

Drupal 6.x では、tokenモジュールに tokenSTRTER という、改造用のソースが付いていて、それを書き換えるだけで実現できていました。

<?php

// $Id: tokenSTARTER.module,v 1.1.2.2 2009/05/14 16:06:47 greggles 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’) { if ($type == ‘global’ || $type == ‘all’) { $tokens[‘global’][‘random-alphanum-11’] = t(‘A randomly generated 11-digit alpha numeric letters.’); $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’: $alpha_num_letters = str_split(‘23456789abcdefghkmnopqrstuvwxyz’); shuffle($alpha_num_letters); $values[‘random-alphanum-11’] = implode(‘’, array_slice($alpha_num_letters, 0, 11)); $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; } </code>

Drupal 7.x では、人様に公開可能なモジュールにしようと、少しまじめに作りました。

random_alpha_numeric_token.info name = Random alpha numeric token description = Provides random alpha numeric token such as 'qIyr73xl.' dependencies[] = token core = 7.x

random_alpha_numeric_token.module <?php /** * @file * Random alpha numeric tokens. * * @ingroup token */

/** * Implements hook_token_info(). */ function random_alpha_numeric_token_token_info() {

$info[‘tokens’][‘random’][‘alphanum’] = array( ‘name’ => t(‘Alpha numeric’), ‘description’ => t(‘A random alpha numeric string. [random:alphanum:8] means random alpha numeric string of length 8. The possible lengths are 4-32 characters.’), ‘dynamic’ => TRUE, );

return $info; }

/** * Implements hook_tokens(). */ function random_alpha_numeric_token_tokens($type, $tokens, array $data = array(), array $options = array()) {

$replacements = array();

if ($type == ‘random’) { // [custom:alphanum:*] dynamic token. if ($hash_tokens = token_find_with_prefix($tokens, ‘alphanum’)) { $alpha_num_letters = str_split(‘1234567890abcdefghijklmnopqrstuvwxyz’); foreach ($hash_tokens as $length => $original) { shuffle($alpha_num_letters); if ($length < 4 || $length > 36) { $length = 8; } $replacements[$original] = implode(‘’, array_slice($alpha_num_letters, 0, $length)); } } }

return $replacements; } </code>

autopath の設定で、node に [random:alphanum:8] とか設定すると、記事を新規に書くたびにランダムな URL が割り当てられます。