You are here

function profanity_tokens in Profanity 7

Implements hook_tokens().

File

./profanity.module, line 186
Main {profanity} file.

Code

function profanity_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);

  // Current page token.
  if ($type == 'current-page') {
    foreach ($tokens as $name => $original) {
      if (strpos($name, 'title-profanity') === 0) {
        $title = drupal_get_title();
        $name_parts = explode(':', $name);
        if (!empty($name_parts[1])) {
          $page_title = profanity_list_execute($name_parts[1], $title);
          $replacements[$original] = $sanitize ? $page_title : decode_entities($page_title);
        }
      }
    }
  }

  // Entity label tokens.
  $entity_info = entity_get_info();

  // If we can't work out a label property, bail out.
  if (empty($entity_info[$type]['entity keys']['label'])) {
    return $replacements;
  }
  $title_property = $entity_info[$type]['entity keys']['label'];
  if (!empty($data[$type]->{$title_property})) {
    foreach ($tokens as $name => $original) {
      if (strpos($name, $title_property . '-profanity') === 0) {
        $name_parts = explode(':', $name);
        if (!empty($name_parts[1])) {
          $replacements[$original] = profanity_list_execute($name_parts[1], $data[$type]->{$title_property});
        }
      }
    }
  }
  return $replacements;
}