token_field.module in Token Field 6
Same filename and directory in other branches
Optional extension to CCK (Content Construction Kit) to provide a read-only field which allows embedding of "compound" fields using Tokens.
File
token_field.moduleView source
<?php
/**
* @file
* Optional extension to CCK (Content Construction Kit) to provide a read-only
* field which allows embedding of "compound" fields using Tokens.
*/
/**
* Implementation of hook_theme().
*/
function token_field_theme() {
return array(
'token_field_formatter_default' => array(
'arguments' => array(
'element' => NULL,
),
),
);
}
/**
* Implementation of hook_field_info().
*/
function token_field_field_info() {
return array(
'token_field' => array(
'label' => t('Token Field'),
'description' => t('A read-only compound field using Tokens.'),
),
);
}
/**
* Implementation of hook_field_settings().
*/
function token_field_field_settings($op, $field) {
switch ($op) {
case 'form':
$form = array();
// Hide multiple and required - this is a read only field
$form['multiple'] = $form['required'] = array(
'#type' => 'value',
);
$form['template'] = array(
'#tree' => TRUE,
);
$form['template']['parse_as_php'] = array(
'#type' => 'checkbox',
'#title' => t('Parse content as PHP?'),
'#description' => '<p>' . t('If this is enabled, the template will be parsed as PHP before any tokens are replaced. This allows you to define <em>optional</em> tokens by wrapping them in if statements.') . '</p>' . '<p>' . t('You will be able to access the <code>$node</code>, <code>$field</code>, <code>$items</code>, <code>$teaser</code> and <code>$page</code> variables, but you will not be able to modify them. You should wrap <code><?php</code> and <code>?></code> wrappers around any PHP code you add.') . '</p>' . '<p>' . t('For example: <code><php if ($node->field_example[0][\'value\'] == \'foo\') : print \'[field_example-formatted] | \'; endif; ?> [field_required_item-formatted]</code>') . '<p>',
'#default_value' => isset($field['template']['parse_as_php']) ? $field['template']['parse_as_php'] : FALSE,
);
$form['template']['body'] = array(
'#type' => 'textarea',
'#default_value' => isset($field['template']['body']) ? $field['template']['body'] : '',
'#description' => t('Using the text area, define the layout for this field using HTML and Tokens.'),
);
$form['template']['format'] = filter_form(isset($field['template']['format']) ? $field['template']['format'] : FILTER_FORMAT_DEFAULT, NULL, array(
'template',
'format',
));
$form['template']['token_help'] = array(
'#type' => 'markup',
'#value' => theme('token_tree', array(
'node',
'global',
)),
);
return $form;
case 'save':
return array(
'template',
);
}
}
/**
* Implementation of hook_field().
*/
function token_field_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'load':
// Get the template from the field and overly the defaults
$item = $field['template'] + array(
'body' => '',
'format' => FILTER_FORMAT_DEFAULT,
'parse_as_php' => FALSE,
);
return array(
$field['field_name'] => array(
$item,
),
);
case 'sanitize':
// Define the available token types
$types = array(
'global' => NULL,
'node' => $node,
);
// For each item, parse the token filtered content as markup
// There will usually only ever be 1 item
foreach ($items as $delta => $item) {
// Parse as PHP first, if set
if ($item['parse_as_php']) {
token_field_parse_as_php($item['body'], $node, $field, $items, $teaser, $page, $delta);
}
// Parse the body for tokens
$body_token_filtered = token_replace_multiple($item['body'], $types);
// Check the parsed token body against the defined input format
$items[$delta]['safe'] = check_markup($body_token_filtered, $item['format'], FALSE);
}
break;
}
}
/**
* Implementation of hook_field_formatter_info().
*/
function token_field_field_formatter_info() {
return array(
'default' => array(
'multiple values' => CONTENT_HANDLE_CORE,
'field types' => array(
'token_field',
),
'label' => t('Default'),
),
);
}
/**
* Implementation of hook_widget_info().
*/
function token_field_widget_info() {
return array(
'token_field' => array(
'label' => t('Token Field (Read Only)'),
'field types' => array(
'token_field',
),
'multiple values' => CONTENT_HANDLE_CORE,
'callbacks' => array(
'default value' => CONTENT_CALLBACK_NONE,
),
),
);
}
/**
* Implementation of hook_widget().
*/
function token_field_widget(&$form, &$form_state, $field, $items, $delta = 0) {
return array();
}
/**
* Implementation of hook_widget_settings().
*/
/*
* Not needed at the moment - keeping for future possible reference
function token_field_widget_settings($op, $widget) {
}
*/
/**
* Implementation of hook_preprocess_content_field()
*/
function token_field_preprocess_content_field(&$vars) {
// If the field is a token_field content field, we add more template
// suggestions so that we can easily theme all Token Fields.
if ($vars['field_type'] == 'field_token') {
$suggestions = array(
'content-field-field-token',
'content-field-field-token-' . $vars['field_name'],
'content-field-field-token-' . $vars['node']->type,
'content-field-field-token-' . $vars['field_name'] . '-' . $vars['node']->type,
);
$vars['template_files'] = array_merge($vars['template_files'], $suggestions);
}
}
/**
* Implementation of hook_content_is_empty().
*/
function token_field_content_is_empty($item, $field) {
return FALSE;
}
/**
* Default formatter - hyperlinked nodes
*/
function theme_token_field_formatter_default($element) {
return $element['#item']['safe'];
}
/**
* This function is used to parse a token_field tempalte as PHP before parsing
* the tokens. It allows site builders to use basic PHP checking to control the
* token output (such as controlling 'lists' of delimited tokens).
*
* The Body is taken by reference, so the function does not return any value,
* instead altering the $body directly.
*
* The rest of the parameters are provided by the token_field_field() function
* and the delta refers to the specific item within $items that is being
* processed.
*
* The intention of using this function is that is "sandboxes" the eval() call
* which helps to avoid items such as $node being altered directly.
*/
function token_field_parse_as_php(&$body, $node, $field, $items, $teaser, $page, $delta) {
ob_start();
print eval('?>' . $body);
$body = ob_get_contents();
ob_end_clean();
}
Functions
Name![]() |
Description |
---|---|
theme_token_field_formatter_default | Default formatter - hyperlinked nodes |
token_field_content_is_empty | Implementation of hook_content_is_empty(). |
token_field_field | Implementation of hook_field(). |
token_field_field_formatter_info | Implementation of hook_field_formatter_info(). |
token_field_field_info | Implementation of hook_field_info(). |
token_field_field_settings | Implementation of hook_field_settings(). |
token_field_parse_as_php | This function is used to parse a token_field tempalte as PHP before parsing the tokens. It allows site builders to use basic PHP checking to control the token output (such as controlling 'lists' of delimited tokens). |
token_field_preprocess_content_field | Implementation of hook_preprocess_content_field() |
token_field_theme | Implementation of hook_theme(). |
token_field_widget | Implementation of hook_widget(). |
token_field_widget_info | Implementation of hook_widget_info(). |