From: Joshua Stewardson Date: May 24, 2012 7:57:21 AM Adding "Term names separated by , or + and converted to Term ID" option to Views diff --git a/modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc b/modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc index c81c619..bdca5e8 100644 --- a/modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc +++ b/modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc @@ -56,6 +56,7 @@ 'tids' => t('Term IDs separated by , or +'), 'name' => t('Term name'), 'convert' => t('Term name converted to Term ID'), + 'convertterms' => t('Term names separated by , or + and converted to Term ID'), ), '#default_value' => $this->options['type'], '#description' => t('Select the form of this filter value; if using term name, it is generally more efficient to convert it to a term ID and use Taxonomy: Term ID rather than Taxonomy: Term Name" as the filter.'), @@ -188,6 +189,87 @@ return TRUE; } return FALSE; + + case 'convertterms': + // An empty argument is not a term so doesn't pass. + if (empty($argument)) { + return FALSE; + } + + $terms = new stdClass(); + $terms->value = $argument; + $terms = views_break_phrase_string($argument, $terms); + if ($terms->value == array(-1)) { + return FALSE; + } + + $convert = drupal_map_assoc($terms->value); + + $query = db_select('taxonomy_term_data', 'td'); + $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid'); + $query->fields('td'); + $query->fields('tv', array('machine_name')); + if (!empty($vocabularies)) { + $query->condition('tv.machine_name', $vocabularies); + } + if ($transform) { + // TODO - change from WHERE to use IN in a Drupal-ized way + //$query->where("replace(td.name, ' ', '-') = :name", array(':name' => $convert)); + } + else { + $query->condition('td.name', $convert); + } + + $result = $query->execute(); + + foreach ($result as $term) { + $test[$term->tid] = $term->tid; + } + + // check, if some tids already verified + static $validated_cache = array(); + foreach ($test as $tid) { + if (isset($validated_cache[$tid])) { + if ($validated_cache[$tid] === FALSE) { + return FALSE; + } + else { + $titles[] = $validated_cache[$tid]; + unset($test[$tid]); + } + } + } + + // if unverified tids left - verify them and cache results + if (count($test)) { + $query = db_select('taxonomy_term_data', 'td'); + $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid'); + $query->fields('td'); + $query->fields('tv', array('machine_name')); + $query->condition('td.tid', $test); + + $result = $query->execute(); + + foreach ($result as $term) { + if ($vocabularies && empty($vocabularies[$term->machine_name])) { + $validated_cache[$term->tid] = FALSE; + return FALSE; + } + + $titles[] = $validated_cache[$term->tid] = check_plain($term->name); + $tids[] = $term->tid; + unset($test[$term->tid]); + } + } + + // Remove duplicate titles and tids + $titles = array_unique($titles); + $tids = array_unique($tids); + + $this->argument->argument = implode($terms->operator == 'or' ? '+' : ',', $tids); // override original argument with converted tids + $this->argument->validated_title = implode($terms->operator == 'or' ? ' + ' : ', ', $titles); + // If this is not empty, we did not find a tid. + return empty($test); } } @@ -195,8 +277,8 @@ $type = $this->options['type']; $transform = $this->options['transform']; $vocabularies = array_filter($this->options['vocabularies']); - - if ($type == 'convert') { + + if ($type == 'convert' || $type == 'convertterms') { $arg_keys = array_flip($args); $query = db_select('taxonomy_term_data', 'td');