diff --git a/modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc b/modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc index 3a88199..57df3ed 100644 --- a/modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc +++ b/modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc @@ -57,6 +57,7 @@ class views_plugin_argument_validate_taxonomy_term extends views_plugin_argument 'tids' => t('Term IDs separated by , or +'), 'name' => t('Term name'), 'convert' => t('Term name converted to Term ID'), + 'convert_multiple' => 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.'), @@ -189,6 +190,97 @@ class views_plugin_argument_validate_taxonomy_term extends views_plugin_argument return TRUE; } return FALSE; + + case 'convert_multiple': + // An empty argument is not a term so doesn't pass. + if (empty($argument)) { + return FALSE; + } + + $terms = new stdClass(); + $terms->value = $argument; + $terms->options['use_plus_separator'] = TRUE; + $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) { + foreach ($convert as &$item) { + // Replace hyphens with spaces. + $item = str_replace('-', ' ', $item); + } + } + $query->condition('td.name', $convert); + + $result = $query->execute(); + + $test = array(); + foreach ($result as $term) { + $test[$term->tid] = $term->tid; + } + + // No terms is not a valid result, so return here. + if (count($test) == 0) { + return FALSE; + } + + // Check if some tids have already been verified. + $tids = array(); + 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]; + $tids[] = $tid; + unset($test[$tid]); + } + } + } + + // If there are 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); } } @@ -197,7 +289,7 @@ class views_plugin_argument_validate_taxonomy_term extends views_plugin_argument $transform = $this->options['transform']; $vocabularies = array_filter($this->options['vocabularies']); - if ($type == 'convert') { + if ($type == 'convert' || $type == 'convert_multiple') { $arg_keys = array_flip($args); $query = db_select('taxonomy_term_data', 'td');