function views_tokens

Implements hook_tokens().

File

./views.tokens.inc, line 45

Code

function views_tokens($type, $tokens, array $data = array(), array $options = array()) {
    $url_options = array(
        'absolute' => TRUE,
    );
    if (isset($options['language'])) {
        $url_options['language'] = $options['language'];
    }
    $sanitize = !empty($options['sanitize']);
    $replacements = array();
    if ($type == 'view' && !empty($data['view'])) {
        $view = $data['view'];
        foreach ($tokens as $name => $original) {
            switch ($name) {
                case 'name':
                    $replacements[$original] = $sanitize ? check_plain($view->human_name) : $view->human_name;
                    break;
                case 'description':
                    $replacements[$original] = $sanitize ? check_plain($view->description) : $view->description;
                    break;
                case 'machine-name':
                    $replacements[$original] = $view->name;
                    break;
                case 'title':
                    $title = $view->get_title();
                    $replacements[$original] = $sanitize ? check_plain($title) : $title;
                    break;
                case 'url':
                    if ($path = $view->get_url()) {
                        $replacements[$original] = url($path, $url_options);
                    }
                    break;
            }
        }
        // [view:url:*] nested tokens. This only works if Token module is installed.
        if ($url_tokens = token_find_with_prefix($tokens, 'url')) {
            if ($path = $view->get_url()) {
                $replacements += token_generate('url', $url_tokens, array(
                    'path' => $path,
                ), $options);
            }
        }
    }
    return $replacements;
}