function ctools_term_context

Discover if this argument gives us the term we crave.

1 string reference to 'ctools_term_context'
term.inc in plugins/arguments/term.inc
Plugin to provide an argument handler for a Taxonomy term.

File

plugins/arguments/term.inc, line 28

Code

function ctools_term_context($arg = NULL, $conf = NULL, $empty = FALSE) {
    // If unset it wants a generic, unfilled context.
    if ($empty) {
        return ctools_context_create_empty('entity:taxonomy_term');
    }
    if (isset($conf['vocabularies'])) {
        $vocabularies = $conf['vocabularies'];
    }
    else {
        $vids = isset($conf['vids']) ? $conf['vids'] : array();
        // Convert legacy use of vids to machine names.
        $vocabularies = _ctools_term_vocabulary_machine_name_convert($vids);
    }
    if (is_object($arg)) {
        $term = $arg;
    }
    else {
        switch ($conf['input_form']) {
            case 'tid':
            default:
                if (!is_numeric($arg)) {
                    return FALSE;
                }
                $term = taxonomy_term_load($arg);
                break;
            case 'term':
                if (!empty($conf['transform'])) {
                    $arg = strtr($arg, '-', ' ');
                }
                $terms = taxonomy_get_term_by_name($arg);
                // If only one term is found, fall through to vocabulary check below.
                if (count($terms) > 1 && $vocabularies) {
                    foreach ($terms as $potential) {
                        foreach ($vocabularies as $machine_name) {
                            if ($potential->vocabulary_machine_name == $machine_name) {
                                $term = $potential;
                                // Break out of the foreaches AND the case.
                                break 3;
                            }
                        }
                    }
                }
                $term = array_shift($terms);
                break;
        }
        if (empty($term)) {
            return NULL;
        }
    }
    if ($vocabularies && !isset($vocabularies[$term->vocabulary_machine_name])) {
        return NULL;
    }
    $context = ctools_context_create('entity:taxonomy_term', $term);
    $context->original_argument = $arg;
    return $context;
}