Index: token.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/token/token.module,v
retrieving revision 1.7.4.9
diff -u -r1.7.4.9 token.module
--- token.module	29 Aug 2008 15:21:40 -0000	1.7.4.9
+++ token.module	15 May 2009 01:40:01 -0000
@@ -158,6 +158,9 @@
   if (module_exists('comment')) {
     require_once("$path/token_comment.inc");
   }
+  if (module_exists('profile')) {
+    require_once("$path/token_profile.inc");
+  }
 
 }
 
--- /dev/null	2009-04-10 13:14:49.395903888 -0500
+++ token_profile.inc	2009-05-14 20:36:20.000000000 -0500
@@ -0,0 +1,106 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Implementations of token module hooks for the core profile module.
+ *
+ * The token module requires specific hooks to be added to modules
+ * so that those modules can return data about their objects to the
+ * token API.  Until and unless token becomes a part of core, the
+ * implementations of the token hooks for core modules are provided
+ * in the token module itself.
+ *
+ * @ingroup token
+ */
+
+/**
+ * Implementation of hook_token_values().
+ */
+function profile_token_values($type, $object = NULL) {
+  $values = array();
+  if ($type == 'node' || $type == 'user') {
+    if (isset($object)) {
+      $account = $object;
+    }
+    else {
+      global $user;
+      $account = $user;
+    }
+
+    profile_load_profile($account);
+    $fields = _profile_token_profile_fields();
+
+    foreach ($fields as $name => $field) {
+      $field->value = $account->$name;
+      $values[$field->name] = _profile_token_format_field($field);
+    }
+  }
+
+  return $values;
+}
+
+/**
+ * Implementation of hook_token_list().
+ */
+function profile_token_list($type = 'all') {
+  if ($type == 'node' || $type == 'user' || $type == 'all') {
+    $fields = _profile_token_profile_fields();
+
+    foreach ($fields as $name => $field) {
+      $tokens['node'][$field->name] = $field->title;
+      $tokens['user'][$field->name] = $field->title;
+    }
+
+    return $tokens;
+  }
+}
+
+function _profile_token_profile_fields() {
+  static $fields;
+
+  if (!isset($fields)) {
+    $fields = array();
+    $result = db_query('SELECT name, title, type FROM {profile_fields}');
+    while ($field = db_fetch_object($result)) {
+      $fields[$field->name] = $field;
+    }
+  }
+
+  return $fields;
+}
+
+function _profile_token_format_field($field) {
+  switch ($field->type) {
+    case 'checkbox':
+      return ($field->value) ? check_plain($field->title) : '';
+    case 'url':
+      return  '<a href="'. check_url($field->value) .'">'. check_plain($field->value) .'</a>';
+    case 'textarea':
+      return check_markup($field->value);
+    case 'date':
+      $format = substr(variable_get('date_format_short', 'm/d/Y - H:i'), 0, 5);
+      // Note: Avoid PHP's date() because it does not handle dates before
+      // 1970 on Windows. This would make the date field useless for e.g.
+      // birthdays.
+      $replace = array('d' => sprintf('%02d', $field->value['day']),
+                       'j' => $field->value['day'],
+                       'm' => sprintf('%02d', $field->value['month']),
+                       'M' => map_month($field->value['month']),
+                       'Y' => $field->value['year'],
+                       'H:i' => NULL,
+                       'g:ia' => NULL);
+      return strtr($format, $replace);
+    case 'list':
+      $values = split("[,\n\r]", $field->value);
+      $fields = array();
+      foreach ($values as $value) {
+        if ($value = trim($value)) {
+          $fields[] = check_plain($value);
+        }
+      }
+      return implode(', ', $fields);
+    default:
+      return check_plain($field->value);
+  }
+}
