function field_default_token_default_value_function in Field default token 7
Function for token replacement of default values.
2 string references to 'field_default_token_default_value_function'
File
- ./
field_default_token.module, line 69 - Enables to use tokens as field default values.
Code
function field_default_token_default_value_function($entity_type, $entity, $field, $instance, $langcode) {
$token_type = $entity_type;
$entity_info = entity_get_info($entity_type);
if (isset($entity_info['token type'])) {
$token_type = $entity_info['token type'];
}
// Do not try to generate tokens from stub entities.
list($entity_id) = entity_extract_ids($entity_type, $entity);
$data = !is_null($entity_id) ? array(
$token_type => $entity,
) : array();
$token_is_label = isset($field['settings']['allowed_values']) && is_array($field['settings']['allowed_values']) && !empty($instance['default_value_label_token']);
if (empty($instance['default_value'])) {
return array();
}
$items = $instance['default_value'];
foreach ($items as &$item) {
if (!is_array($item)) {
$item = token_replace($item, $data, array(
'clear' => TRUE,
'sanitize' => FALSE,
));
if ($token_is_label) {
$item = array_search($item, $field['settings']['allowed_values']);
}
}
else {
foreach ($item as $column_name => $column_value) {
if (is_array($column_value)) {
// Complex fields such as Table Field contain a structure inside the value, so we can't replace it with a string.
continue;
}
$item[$column_name] = token_replace($column_value, $data, array(
'clear' => TRUE,
'sanitize' => FALSE,
));
if ($token_is_label) {
$item[$column_name] = array_search($item[$column_name], $field['settings']['allowed_values']);
}
}
}
}
return $items;
}