You are here

function cas_attributes_tokens in CAS Attributes 7

Same name and namespace in other branches
  1. 8 cas_attributes.tokens.inc \cas_attributes_tokens()
  2. 2.x cas_attributes.tokens.inc \cas_attributes_tokens()

Implements hook_tokens().

File

./cas_attributes.tokens.inc, line 24
Token module integration.

Code

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

    // Provide [cas:attribute:?] dynamic tokens.
    if ($attribute_tokens = token_find_with_prefix($tokens, 'attribute')) {
      $attribute = array_change_key_case(cas_phpcas_attributes($cas));
      foreach ($attribute_tokens as $name => $original) {

        // If there are no options specified have it return all values.
        if (strpos($name, ':') === FALSE) {
          $name .= ':join';
        }

        // Break out the token into attributes and the options for them.
        list($name, $token) = explode(':', $name, 2);
        $name = drupal_strtolower($name);
        if (isset($attribute[$name])) {
          $value = $attribute[$name];
          if (!is_array($value)) {
            $value = array(
              $value,
            );
          }
          $replacements += token_generate('array', array(
            $token => $original,
          ), array(
            'array' => $value,
          ), $options);
        }
        elseif ($name == '?') {
          $keys = array_keys($attribute);
          if ($sanitize) {
            $keys = array_map('check_plain', $keys);
          }
          $replacements[$original] = t('Available attributes: %keys', array(
            '%keys' => implode(', ', $keys),
          ));
        }
      }
    }
  }
  return $replacements;
}