You are here

function _aet_entity_view_tokens in Advanced Entity Tokens 7

This is the main implementation of hook_tokens.

The main purpose of this hook implementation is to identify the entity type and call aet_entity_token on it.

Parameters

string $type: The tokens type.

array $tokens: A list of tokens to replace.

array $data: Additional data for this function (e.g. an entity object). This function doesn't need additional data though.

array $options: An associative array of options for token replacement; see token_replace() for possible values.

1 call to _aet_entity_view_tokens()
_aet_entity_tokens in ./aet.tokens.inc
INTERNAL implementation of hook_tokens specified for entities.

File

./aet.tokens.inc, line 285
Token callbacks for the AET module.

Code

function _aet_entity_view_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();

  // Gets the information array on all the available entities in this drupal
  // site.
  $entities = entity_get_info();
  $entity_type = '';
  $entity_info = NULL;
  foreach ($entities as $key => $value) {
    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) && !empty($data[$type])) {
    $entity = $data[$type];
    foreach ($entity_info['view modes'] as $key => $value) {
      if (!empty($tokens['view-' . $key])) {

        // Renames $key to $view_mode for better readability.
        $view_mode = $key;

        // Get the renderable array of the entity with entity_view.
        $renderable_entity = entity_view($entity_type, array(
          $entity,
        ), $view_mode);

        // Render the entity.
        $rendered_entity = render($renderable_entity);

        // Add the rendered entity to the replacements array.
        $replacements[$tokens['view-' . $key]] = $rendered_entity;
      }
    }
  }

  // Returns the $replacements array.
  return $replacements;
}