diff -u b/serial.module b/serial.module --- b/serial.module +++ b/serial.module @@ -112,35 +112,57 @@ } } -// Tokens for fields are currently not supported - http://drupal.org/node/691078. - -///** -// * Implements hook_token_info(). -// */ +/** + * Implements hook_token_info(). + * + * Here are examples how to use token with serial field: + * node:field-serial-name or user:field-serial-name-2 + */ //function serial_token_info() { -// $type = array( -// 'name' => t('Nodes'), -// 'description' => t('Tokens related to individual nodes.'), -// 'needs-data' => 'node', -// ); -// $node['serial'] = array( -// 'name' => t("Serial Field"), -// 'description' => t('Serial field value (unique per node type)'), -// 'needs-data' => 'node', -// ); -// return array( -// 'types' => array('node' => $type), -// 'tokens' => array('node' => $node), -// ); -//} -// -///** -// * Implements hook_tokens(). -// */ -//function serial_tokens($type, $tokens, $data, $options) { -// // TODO +// No need to define new types or tokens. //} +/** + * Implements hook_tokens(). + * + * Replace token for generic entity type. + */ +function serial_tokens($type, $tokens, $data, $options) { + $replacements = array(); + $sanitize = !empty($options['sanitize']); + + $entity_info = entity_get_info($type); + // Make sure type is entity type. + if (empty($entity_info)) { + return $replacements; + } + + $entity_type = $type; + if (!empty($data[$entity_type])) { + // Generic entity. + $entity = $data[$entity_type]; + + foreach ($tokens as $name => $original) { + // Convert token to field name. + $field_name = str_replace('-', '_', $name); + + $field_info = field_info_field($field_name); + if ($field_info['type'] == 'serial') { + $items = field_get_items($entity_type, $entity, $field_name); + if (empty($items)) { + continue; + } + + $item = reset($items); + $serial_value = $item['value']; + $replacements[$original] = $sanitize ? filter_xss($serial_value) : $serial_value; + } + } + } + + return $replacements; +} + //=================// // Field Formatter // //=================//