function entity_token_tokens in Entity API 7
Implements hook_tokens().
File
- ./
entity_token.tokens.inc, line 168 - Provides tokens for entity properties which have no token yet.
Code
function entity_token_tokens($type, $tokens, array $data = array(), array $options = array()) {
$token_types = entity_token_types_chained();
$replacements = array();
if (isset($token_types[$type]) && (!empty($data[$type]) || $type == 'site')) {
$data += array(
$type => FALSE,
);
// Make use of token module's token cache if available.
$info = module_exists('token') ? token_get_info() : token_info();
foreach ($tokens as $name => $original) {
// Provide the token for all properties marked to stem from us.
if (!empty($info['tokens'][$type][$name]['entity-token']) || $type == 'struct') {
$wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, $token_types[$type], $data[$type], $options) : $wrapper;
$property_name = str_replace('-', '_', $name);
try {
if (isset($wrapper->{$property_name})) {
$replacement = _entity_token_get_token($wrapper->{$property_name}, $options);
if (isset($replacement)) {
$replacements[$original] = $replacement;
}
}
} catch (EntityMetadataWrapperException $e) {
// If tokens for not existing values are requested, just do nothing.
}
}
}
// Properly chain everything of a type marked as needs chaining.
$info['tokens'] += array(
$type => array(),
);
foreach ($info['tokens'][$type] as $name => $token_info) {
if (!empty($token_info['entity-token']) && isset($token_info['type']) && entity_token_types_chained($token_info['type'])) {
if ($chained_tokens = token_find_with_prefix($tokens, $name)) {
$wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, $token_types[$type], $data[$type], $options) : $wrapper;
$property_name = str_replace('-', '_', $name);
try {
// Pass on 'struct' properties wrapped, else un-wrap the data.
$value = $token_info['type'] == 'struct' ? $wrapper->{$property_name} : $wrapper->{$property_name}
->value();
$replacements += token_generate($token_info['type'], $chained_tokens, array(
$token_info['type'] => $value,
), $options);
} catch (EntityMetadataWrapperException $e) {
// If tokens for not existing values are requested, just do nothing.
}
}
}
}
}
elseif ($item_token_type = entity_property_list_extract_type($type)) {
foreach ($tokens as $name => $original) {
// Care about getting entries of a list.
if (is_numeric($name)) {
$wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, "list<{$token_types[$item_token_type]}>", $data[$type], $options) : $wrapper;
try {
$replacement = _entity_token_get_token($wrapper
->get($name), $options);
if (isset($replacement)) {
$replacements[$original] = $replacement;
}
} catch (EntityMetadataWrapperException $e) {
// If tokens for not existing values are requested, just do nothing.
}
}
else {
$parts = explode(':', $name, 2);
$delta = $parts[0];
if (is_numeric($delta) && ($chained_tokens = token_find_with_prefix($tokens, $delta))) {
$wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, "list<{$token_types[$item_token_type]}>", $data[$type], $options) : $wrapper;
try {
$replacements += token_generate($item_token_type, $chained_tokens, array(
$item_token_type => $wrapper
->get($delta)
->value(),
), $options);
} catch (EntityMetadataWrapperException $e) {
// If tokens for not existing values are requested, just do nothing.
}
}
}
}
}
// Add support for chaining struct data. As struct data has no registered
// tokens, we have to chain based upon wrapper property info.
if ($type == 'struct') {
$wrapper = $data[$type];
foreach ($wrapper as $name => $property) {
$token_type = _entity_token_map_to_token_type($property
->info());
if (entity_token_types_chained($token_type) && ($chained_tokens = token_find_with_prefix($tokens, $name))) {
try {
// Pass on 'struct' properties wrapped, else un-wrap the data.
$value = $token_type == 'struct' ? $property : $property
->value();
$replacements += token_generate($token_type, $chained_tokens, array(
$token_type => $value,
), $options);
} catch (EntityMetadataWrapperException $e) {
// If tokens for not existing values are requested, just do nothing.
}
}
}
}
return $replacements;
}