machine_name] = check_plain($voc->name); } $form['vocabularies'] = array( '#type' => 'checkboxes', '#prefix' => '
', '#suffix' => '
', '#title' => t('Vocabularies'), '#options' => $options, '#default_value' => $this->options['vocabularies'], '#description' => t('If you wish to validate for specific vocabularies, check them; if none are checked, all terms will pass.'), ); $form['type'] = array( '#type' => 'select', '#title' => t('Filter value type'), '#options' => array( 'tid' => t('Term ID'), '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 "," 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.'), ); $form['transform'] = array( '#type' => 'checkbox', '#title' => t('Transform dashes in URL to spaces in term name filter values'), '#default_value' => $this->options['transform'], ); } function validate_argument($argument) { $vocabularies = array_filter($this->options['vocabularies']); $type = $this->options['type']; $transform = $this->options['transform']; switch ($type) { case 'tid': if (!is_numeric($argument)) { return FALSE; } $query = db_select('taxonomy_term_data', 'td'); $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid'); $query->fields('td'); $query->condition('td.tid', $argument); $query->addTag('term_access'); $term = $query->execute()->fetchObject(); if (!$term) { return FALSE; } $term = taxonomy_term_load($term->tid); $this->argument->validated_title = check_plain(entity_label('taxonomy_term', $term)); return empty($vocabularies) || !empty($vocabularies[$term->vocabulary_machine_name]); case 'tids': // An empty argument is not a term so doesn't pass. if (empty($argument)) { return FALSE; } $tids = new stdClass(); $tids->value = $argument; $tids = views_break_phrase($argument, $tids); if ($tids->value == array(-1)) { return FALSE; } $test = drupal_map_assoc($tids->value); $titles = array(); // 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; } $term = taxonomy_term_load($term->tid); $titles[] = $validated_cache[$term->tid] = check_plain(entity_label('taxonomy_term', $term)); unset($test[$term->tid]); } } // Remove duplicate titles $titles = array_unique($titles); $this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles); // If this is not empty, we did not find a tid. return empty($test); case 'name': case 'convert': $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) { $query->where("replace(td.name, ' ', '-') = :name", array(':name' => $argument)); } else { $query->condition('td.name', $argument); } $term = $query->execute()->fetchObject(); if ($term && (empty($vocabularies) || !empty($vocabularies[$term->machine_name]))) { if ($type == 'convert') { $this->argument->argument = $term->tid; } $term = taxonomy_term_load($term->tid); $this->argument->validated_title = check_plain(entity_label('taxonomy_term', $term)); return TRUE; } return FALSE; case 'convert_multiple': // An empty argument is not a term so doesn't pass. if (empty($argument)) { return FALSE; } //$argument = 'education,fitness'; $terms = new stdClass(); $terms->value = $argument; $terms->options['use_plus_separator'] = TRUE; //Replace spaces with pipe $argument = str_replace(' ', ',', $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); } // Reverse the pipe replacement. foreach ($convert as &$item) { $item = str_replace('|', ' ', $item); } if ($transform) { foreach ($convert as &$item) { // Replace hyphens with spaces. $item = str_replace('-', ' ', $item); } } $query->condition('td.name', $convert); //print_r($query); $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); } } }