You are here

function workbench_access_tokens in Workbench Access 7

Same name and namespace in other branches
  1. 8 workbench_access.module \workbench_access_tokens()

Implements hook_tokens().

File

./workbench_access.tokens.inc, line 58
Token integration for the workbench_access module.

Code

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

  // Node tokens.
  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'workbench-access-sections':
          if ($sections = _workbench_access_get_node_section_names($node)) {
            if ($sanitize) {

              // Run sections through check_plain() if they should be sanitized.
              $sections = array_map('check_plain', $sections);
            }
            $join = isset($options['join']) ? $options['join'] : ', ';
            $replacements[$original] = implode($join, $sections);
          }
          elseif (variable_get('workbench_access_node_type_' . $node->type, 1)) {

            // Output the default unassigned token if the content type is access
            // controlled.
            $replacements[$original] = t('Unassigned');
          }
          break;
      }
    }

    // Chained token relationships.
    if ($section_tokens = token_find_with_prefix($tokens, 'workbench-access-sections')) {
      if ($sections = _workbench_access_get_node_section_names($node)) {
        $replacements += token_generate('array', $section_tokens, array(
          'array' => $sections,
        ), $options);
      }
    }
  }

  // User tokens.
  if ($type == 'user' && !empty($data['user'])) {
    $account = $data['user'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'workbench-access-sections':
          if ($sections = _workbench_access_get_user_section_names($account)) {
            if ($sanitize) {

              // Run sections through check_plain() if they should be sanitized.
              $sections = array_map('check_plain', $sections);
            }
            $join = isset($options['join']) ? $options['join'] : ', ';
            $replacements[$original] = implode($join, $sections);
          }
          break;
      }
    }

    // Chained token relationships.
    if ($section_tokens = token_find_with_prefix($tokens, 'workbench-access-sections')) {
      if ($sections = _workbench_access_get_user_section_names($account)) {
        $replacements += token_generate('array', $section_tokens, array(
          'array' => $sections,
        ), $options);
      }
    }
  }

  // Site global tokens.
  if ($type == 'site') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'workbench-access-scheme':
          if ($scheme = workbench_access_access_scheme_load(variable_get('workbench_access'))) {
            $replacements[$original] = $sanitize ? check_plain($scheme['name']) : $scheme['name'];
          }
          break;
      }
    }

    // Chained token relationships.
    if ($scheme_tokens = token_find_with_prefix($tokens, 'workbench-access-scheme')) {
      if ($scheme = workbench_access_access_scheme_load(variable_get('workbench_access'))) {
        $replacements += token_generate('workbench-access-scheme', $scheme_tokens, array(
          'workbench-access-scheme' => $scheme,
        ), $options);
      }
    }
  }

  // Workbench access scheme tokens.
  if ($type == 'workbench-access-scheme' && !empty($data['workbench-access-scheme'])) {
    $scheme = $data['workbench-access-scheme'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'name':
          $replacements[$original] = $sanitize ? check_plain($scheme['name']) : $scheme['name'];
          break;
        case 'machine-name':

          // Machine names are assumed to not contain unsafe characters.
          $replacements[$original] = $scheme['access_scheme'];
          break;
        case 'description':
          $replacements[$original] = $sanitize ? check_plain($scheme['description']) : $scheme['description'];
          break;
      }
    }
  }
  return $replacements;
}