View source
<?php
define('TOKEN_CUSTOM_DEFAULT_TYPE', 'custom');
function token_custom_help($path, $arg) {
switch ($path) {
case 'admin/help#token_custom':
$output = '<p>' . t("This allows you to create custom tokens for use throughtout the site using the <a href='@admin_url'>administration pages</a>", array(
'@admin_url' => url('admin/structure/token-custom'),
)) . '</p>';
$output .= '<p>' . t('Tokens can be created using markup or PHP and have access to context related objects.') . '</p>';
return $output;
}
}
function token_custom_permission() {
return array(
'administer custom tokens' => array(
'title' => t('Manage custom tokens using PHP.'),
'description' => t('Create/edit/delete custom tokens using markup or PHP.'),
'restrict access' => TRUE,
),
'list custom tokens' => array(
'title' => t("Access the custom token's administration page."),
'description' => t('View custom tokens.'),
),
);
}
function token_custom_menu() {
$items['admin/structure/token-custom'] = array(
'title' => 'Custom tokens',
'description' => 'Administrate custom tokens.',
'page callback' => 'token_custom_list_page',
'access arguments' => array(
'list custom tokens',
),
'file' => 'token_custom.admin.inc',
);
$items['admin/structure/token-custom/list'] = array(
'title' => 'Custom tokens',
'description' => 'List of custom tokens.',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/structure/token-custom/add'] = array(
'title' => 'Add token',
'description' => 'Create custom tokens',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'token_custom_edit_form',
'add',
),
'access arguments' => array(
'administer custom tokens',
),
'file' => 'token_custom.admin.inc',
'weight' => 1,
'type' => MENU_LOCAL_ACTION,
);
$items['admin/structure/token-custom/%token_custom/edit'] = array(
'title' => 'Edit token',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'token_custom_edit_form',
'edit',
3,
),
'access arguments' => array(
'administer custom tokens',
),
'file' => 'token_custom.admin.inc',
'type' => MENU_CALLBACK,
'weight' => 10,
);
$items['admin/structure/token-custom/%token_custom/delete'] = array(
'title' => 'Delete custom token',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'token_custom_delete_confirm_form',
3,
),
'access arguments' => array(
'administer custom tokens',
),
'file' => 'token_custom.admin.inc',
);
$items['admin/structure/token-custom/type'] = array(
'title' => 'Custom token types',
'description' => 'View custom token types',
'page callback' => 'token_custom_type_list_page',
'access arguments' => array(
'administer custom tokens',
),
'file' => 'token_custom.admin.inc',
'weight' => 2,
'type' => MENU_LOCAL_TASK,
);
$items['admin/structure/token-custom/type/add'] = array(
'title' => 'Add token type',
'description' => 'Create custom token types',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'token_custom_type_edit_form',
'add',
),
'access arguments' => array(
'administer custom tokens',
),
'file' => 'token_custom.admin.inc',
'weight' => 2,
'type' => MENU_LOCAL_ACTION,
);
$items['admin/structure/token-custom/type/%token_custom_type/edit'] = array(
'title' => 'Edit token type',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'token_custom_type_edit_form',
'edit',
4,
),
'access callback' => 'token_custom_type_forms_access',
'access arguments' => array(
4,
),
'file' => 'token_custom.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/structure/token-custom/type/%token_custom_type/delete'] = array(
'title' => 'Delete custom token',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'token_custom_type_delete_confirm_form',
4,
),
'access callback' => 'token_custom_type_forms_access',
'access arguments' => array(
4,
),
'file' => 'token_custom.admin.inc',
);
return $items;
}
function token_custom_token_info() {
$token_info = array();
foreach (token_custom_type_load_multiple() as $type) {
$token_info['types'][$type->machine_name] = array(
'name' => $type->name,
'description' => $type->description,
);
}
foreach (token_custom_load_multiple() as $token) {
$token_info['tokens'][$token->type][$token->machine_name] = array(
'name' => $token->name,
'description' => $token->description,
);
}
return $token_info;
}
function token_custom_tokens($type, $tokens, array $data = array(), array $options = array()) {
$return = array();
$custom_tokens = token_custom_load_multiple(array_keys($tokens));
foreach ($custom_tokens as $machine_name => $custom_token) {
if ($type == $custom_token->type && array_key_exists($machine_name, $tokens)) {
$return[$tokens[$machine_name]] = token_custom_token_render($machine_name, $custom_token->content, $data, $options);
}
}
return $return;
}
function token_custom_type_forms_access($type) {
return user_access('administer custom tokens') && $type->machine_name != TOKEN_CUSTOM_DEFAULT_TYPE;
}
function token_custom_type_load_multiple() {
$default_type = new stdClass();
$default_type->name = t('Custom');
$default_type->machine_name = TOKEN_CUSTOM_DEFAULT_TYPE;
$default_type->description = t('User created tokens types using the Custom Tokens module.');
$types = variable_get('token_custom_types', array());
if (!isset($types[TOKEN_CUSTOM_DEFAULT_TYPE])) {
$types[TOKEN_CUSTOM_DEFAULT_TYPE] = $default_type;
}
return $types;
}
function token_custom_type_load($type) {
$types = token_custom_type_load_multiple();
return isset($types[$type]) ? $types[$type] : FALSE;
}
function token_custom_type_save($type) {
if (!is_object($type)) {
$type = (object) $type;
}
if ($type->machine_name == TOKEN_CUSTOM_DEFAULT_TYPE) {
return FALSE;
}
$types = token_custom_type_load_multiple();
$types[$type->machine_name] = $type;
variable_set('token_custom_types', $types);
return $type;
}
function token_custom_type_delete($delete_type) {
$types = token_custom_type_load_multiple();
if (empty($delete_type) || !isset($types[$delete_type])) {
return FALSE;
}
if ($delete_type == TOKEN_CUSTOM_DEFAULT_TYPE) {
return FALSE;
}
$tokens = token_custom_load_multiple();
foreach ($tokens as $token) {
if ($token->type == $delete_type) {
$token->type = TOKEN_CUSTOM_DEFAULT_TYPE;
$token->is_new = FALSE;
token_custom_save($token);
}
}
unset($types[$delete_type]);
variable_set('token_custom_types', $types);
if (module_exists('token')) {
token_clear_cache();
}
return TRUE;
}
function token_custom_save($token) {
$is_new = is_object($token) ? (bool) $token->is_new : !empty($token['is_new']);
$return = (bool) drupal_write_record('token_custom', $token, $is_new ? array() : 'machine_name');
if (module_exists('token')) {
token_clear_cache();
}
return $return;
}
function token_custom_load($machine_name = NULL) {
if (empty($machine_name) || !is_string($machine_name)) {
return NULL;
}
$tokens = token_custom_load_multiple(array(
$machine_name,
));
return array_shift($tokens);
}
function token_custom_load_multiple($names = NULL) {
static $tokens = array();
static $all_loaded = FALSE;
if ($names === NULL) {
if (!$all_loaded) {
$loaded = array_keys($tokens);
$query = db_select('token_custom')
->fields('token_custom');
if (!empty($loaded)) {
$query
->condition('machine_name', $loaded, 'NOT IN');
}
$results = $query
->execute();
$all_loaded = TRUE;
foreach ($results as $token) {
$tokens[$token->machine_name] = $token;
}
}
return $tokens;
}
$to_fetch = array();
foreach ($names as $name) {
if (!array_key_exists($name, $tokens)) {
$to_fetch[] = $name;
}
}
if (!empty($to_fetch)) {
$query = db_select('token_custom')
->fields('token_custom')
->condition('machine_name', $to_fetch, 'IN');
$results = $query
->execute();
foreach ($results as $token) {
$tokens[$token->machine_name] = $token;
}
}
$return = array();
foreach ($names as $name) {
if (isset($tokens[$name])) {
$return[$name] = $tokens[$name];
}
}
return $return;
}
function token_custom_delete($machine_name) {
if (!is_string($machine_name) || empty($machine_name)) {
return FALSE;
}
$return = db_delete('token_custom')
->condition('machine_name', $machine_name)
->execute();
if (module_exists('token')) {
token_clear_cache();
}
return $return;
}
function token_custom_token_render($machine_name, $code, $data, $options) {
$token = token_custom_load($machine_name);
$format = isset($token->format) ? $token->format : NULL;
if ($format == 'php_code') {
$static_count =& drupal_static('token_custom_data:counter', 0);
$static_key = 'token_custom_data:' . $static_count;
$static_count++;
drupal_static($static_key, array(
'data' => $data,
'options' => $options,
));
$code = '
<?php
if ($static = drupal_static(\'' . $static_key . '\')) {
extract($static);
} ?>' . $code;
$output = check_markup($code, $format);
drupal_static_reset($static_key);
return $output;
}
else {
return check_markup($code, $format);
}
}
function token_custom_features_api() {
return array(
'token_custom' => array(
'name' => 'Custom tokens',
'file' => drupal_get_path('module', 'token_custom') . '/token_custom.features.inc',
'default_hook' => 'token_custom_features_default_settings',
'feature_source' => TRUE,
),
);
}