function _aet_entity_tokens in Advanced Entity Tokens 7
INTERNAL implementation of hook_tokens specified for entities.
This function implements the entity level of AET. E.G. [aet:node:1] [aet:file:label]
Parameters
string $type: The tokens type. This is also the entity type.
array $tokens: A list of tokens to replace.
array $data: Additional data for this function.
array $options: An associative array of options for token replacement; see token_replace() for possible values.
1 call to _aet_entity_tokens()
- aet_tokens in ./
aet.tokens.inc - This is the main implementation of hook_tokens.
File
- ./
aet.tokens.inc, line 137 - Token callbacks for the AET module.
Code
function _aet_entity_tokens($type, $tokens, array $data = array(), array $options = array()) {
// Initiating the $replacements array.
$replacements = array();
// TODO Add support $options['language']
// TODO Add support $options['callback']
// TODO Add support $options['sanitize']
$options += _aet_default_options();
$entities = entity_get_info();
$entity_type = '';
$entity_info = NULL;
foreach ($entities as $key => $value) {
// Gets the information array on all the available entities in this drupal
// site.
if (isset($value['token type']) && $value['token type'] === $type) {
$entity_type = $key;
$entity_info = $value;
}
}
// Checks if $entity_info is not empty.
if (!empty($entity_info)) {
// Loops over the $tokens array.
foreach ($tokens as $key => $original) {
// The position of the first colon is used for both checking if a secound
// part to the key exists and to seperate between it (the first part) and
// the rest of the key.
$first_colon_pos = strpos($key, ':');
// The $first_key is the part of the key until the first colon if exist,
// or the full $key if no colon exists.
$first_key = $first_colon_pos ? substr($key, 0, $first_colon_pos) : $key;
// Checks if the $first_key is numeric - the entity id.
if (_aet_is_valid($first_key)) {
// Renames the $first_key as $entity_id for better readability.
$entity_id = $first_key;
$id_key = $entity_info['entity keys']['id'];
// Check if the entity to load is currently being viewed or if the
// the rendered token origins from itself.
if (aet_entity_is_page($entity_id, $id_key) || aet_entity_is_viewed($entity_type, $id_key, $entity_id)) {
// To avoid recursion we skip this entity.
drupal_set_message(t('Recursive token use detected.'), 'warning');
continue;
}
// Loads the entity. entity_loads loads the entity into an assosative
// array with the $entity_id as the array key. we use array_values to
// move the entities array into a simple array.
$entities_array = is_numeric($entity_id) ? array_values(entity_load($entity_type, array(
$entity_id,
))) : (AET_UUID ? array_values(entity_uuid_load($entity_type, array(
$entity_id,
))) : array());
// Sometimes there are no instances of the entity type.
if (count($entities_array) > 0) {
// The entity object.
$entity = $entities_array[0];
global $user;
// Gets the access callback for this entity.
$access_callback = !empty($entity_info['access callback']) ? $entity_info['access callback'] : '_aet_empty_function_';
// Checks if the user has access to view this entity.
if (function_exists($access_callback) && !$access_callback('view', $entity, $user, $entity_type)) {
continue;
}
// Checks if the $tokens array contain the $entity_id as one of the
// "to be replaced" tokens.
if (isset($tokens[$entity_id])) {
// The $view_mode of the entity will default to token if exists or
// full if it doesn't. use another chaining level to specify the
// display.
$view_mode = isset($entity_info['view modes']['token']) ? 'token' : 'full';
// Get the renderable array of the entity with entity_view.
$renderable_entity = entity_view($entity_type, $entities_array, $view_mode);
// Render the entity.
$rendered_entity = render($renderable_entity);
// Add the rendered entity to the replacements array.
$replacements[$tokens[$entity_id]] = $rendered_entity;
}
$replacements += _aet_entity_view_tokens($type, token_find_with_prefix($tokens, $entity_id), array(
$type => $entity,
), $options);
// I'm calling the token_generate to the rest of the chaining at this
// point - after the entity type and nid have been figured out.
$replacements += token_generate($type, token_find_with_prefix($tokens, $entity_id), array(
$type => $entity,
), $options);
}
}
}
// Calls aet_clear_tokens to clear unreplaced tokens if the option is
// flagged.
aet_clear_tokens($tokens, $replacements, $options['clear']);
}
// Returns the $replacements array.
return $replacements;
}