You are here

function title_tokens_alter in Title 7

Implements hook_tokens_alter().

Make sure tokens are properly translated.

File

./title.module, line 750

Code

function title_tokens_alter(array &$replacements, array $context) {
  $mapping =& drupal_static(__FUNCTION__);
  if (empty($mapping)) {
    foreach (entity_get_info() as $entity_type => $info) {
      if (!empty($info['token type'])) {
        $mapping[$info['token type']] = $entity_type;
      }
    }
  }
  if (isset($mapping[$context['type']])) {
    $entity_type = $mapping[$context['type']];
    $fr_info = title_field_replacement_info($entity_type);
    if ($fr_info && !empty($context['data'][$context['type']])) {
      $entity = $context['data'][$context['type']];
      list(, , $bundle) = entity_extract_ids($entity_type, $entity);
      $options = $context['options'];

      // Since Title tokens are mostly used in storage contexts we default to
      // the current working language, that is the entity language. Modules
      // using Title tokens in display contexts need to specify the current
      // display language.
      $langcode = isset($options['language']) ? $options['language']->language : entity_language($entity_type, $entity);
      if ($fr_info) {
        foreach ($fr_info as $legacy_field => $info) {
          if (isset($context['tokens'][$legacy_field]) && title_field_replacement_enabled($entity_type, $bundle, $legacy_field)) {
            $langcode = field_language($entity_type, $entity, $info['field']['field_name'], $langcode);
            $item = $info['callbacks']['sync_get']($entity_type, $entity, $legacy_field, $info, $langcode);
            if (!empty($item)) {
              list($value, $format) = array_values($item);
              if (empty($format)) {
                $replacements[$context['tokens'][$legacy_field]] = check_plain($value);
              }
              else {
                $replacements[$context['tokens'][$legacy_field]] = check_markup($value, $format, $langcode);
              }
            }
          }
        }
      }
    }
  }
}