views_plugin_argument_validate_taxonomy_term.inc

  1. 3.x modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc
  2. 2.x modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc

Contains the 'taxonomy term' argument validator plugin.

File

modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains the 'taxonomy term' argument validator plugin.
  5. */
  6. /**
  7. * Validate whether an argument is an acceptable node.
  8. */
  9. class views_plugin_argument_validate_taxonomy_term extends views_plugin_argument_validate {
  10. function init(&$view, &$argument, $options) {
  11. parent::init($view, $argument, $options);
  12. // Convert legacy vids option to machine name vocabularies.
  13. if (!empty($this->options['vids'])) {
  14. $vocabularies = taxonomy_get_vocabularies();
  15. foreach ($this->options['vids'] as $vid) {
  16. if (isset($vocabularies[$vid], $vocabularies[$vid]->machine_name)) {
  17. $this->options['vocabularies'][$vocabularies[$vid]->machine_name] = $vocabularies[$vid]->machine_name;
  18. }
  19. }
  20. }
  21. }
  22. function option_definition() {
  23. $options = parent::option_definition();
  24. $options['vocabularies'] = array('default' => array());
  25. $options['type'] = array('default' => 'tid');
  26. $options['transform'] = array('default' => FALSE, 'bool' => TRUE);
  27. return $options;
  28. }
  29. function options_form(&$form, &$form_state) {
  30. $vocabularies = taxonomy_get_vocabularies();
  31. $options = array();
  32. foreach ($vocabularies as $voc) {
  33. $options[$voc->machine_name] = check_plain($voc->name);
  34. }
  35. $form['vocabularies'] = array(
  36. '#type' => 'checkboxes',
  37. '#prefix' => '<div id="edit-options-validate-argument-vocabulary-wrapper">',
  38. '#suffix' => '</div>',
  39. '#title' => t('Vocabularies'),
  40. '#options' => $options,
  41. '#default_value' => $this->options['vocabularies'],
  42. '#description' => t('If you wish to validate for specific vocabularies, check them; if none are checked, all terms will pass.'),
  43. );
  44. $form['type'] = array(
  45. '#type' => 'select',
  46. '#title' => t('Filter value type'),
  47. '#options' => array(
  48. 'tid' => t('Term ID'),
  49. 'tids' => t('Term IDs separated by , or +'),
  50. 'name' => t('Term name'),
  51. 'convert' => t('Term name converted to Term ID'),
  52. ),
  53. '#default_value' => $this->options['type'],
  54. '#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.'),
  55. );
  56. $form['transform'] = array(
  57. '#type' => 'checkbox',
  58. '#title' => t('Transform dashes in URL to spaces in term name filter values'),
  59. '#default_value' => $this->options['transform'],
  60. );
  61. }
  62. function options_submit(&$form, &$form_state, &$options = array()) {
  63. // Filter unselected items so we don't unnecessarily store giant arrays.
  64. $options['vocabularies'] = array_filter($options['vocabularies']);
  65. }
  66. function convert_options(&$options) {
  67. if (!isset($options['vocabularies']) && !empty($this->argument->options['validate_argument_vocabulary'])) {
  68. $options['vocabularies'] = $this->argument->options['validate_argument_vocabulary'];
  69. $options['type'] = $this->argument->options['validate_argument_type'];
  70. $options['transform'] = isset($this->argument->options['validate_argument_transform']) ? $this->argument->options['validate_argument_transform'] : FALSE;
  71. }
  72. }
  73. function validate_argument($argument) {
  74. $vocabularies = array_filter($this->options['vocabularies']);
  75. $type = $this->options['type'];
  76. $transform = $this->options['transform'];
  77. switch ($type) {
  78. case 'tid':
  79. if (!is_numeric($argument)) {
  80. return FALSE;
  81. }
  82. $query = db_select('taxonomy_term_data', 'td');
  83. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  84. $query->fields('td');
  85. $query->condition('td.tid', $argument);
  86. $query->addTag('term_access');
  87. $term = $query->execute()->fetchObject();
  88. if (!$term) {
  89. return FALSE;
  90. }
  91. $term = taxonomy_term_load($term->tid);
  92. $this->argument->validated_title = check_plain(entity_label('taxonomy_term', $term));
  93. return empty($vocabularies) || !empty($vocabularies[$term->vocabulary_machine_name]);
  94. case 'tids':
  95. // An empty argument is not a term so doesn't pass.
  96. if (empty($argument)) {
  97. return FALSE;
  98. }
  99. $tids = new stdClass();
  100. $tids->value = $argument;
  101. $tids = views_break_phrase($argument, $tids);
  102. if ($tids->value == array(-1)) {
  103. return FALSE;
  104. }
  105. $test = drupal_map_assoc($tids->value);
  106. $titles = array();
  107. // check, if some tids already verified
  108. static $validated_cache = array();
  109. foreach ($test as $tid) {
  110. if (isset($validated_cache[$tid])) {
  111. if ($validated_cache[$tid] === FALSE) {
  112. return FALSE;
  113. }
  114. else {
  115. $titles[] = $validated_cache[$tid];
  116. unset($test[$tid]);
  117. }
  118. }
  119. }
  120. // if unverified tids left - verify them and cache results
  121. if (count($test)) {
  122. $query = db_select('taxonomy_term_data', 'td');
  123. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  124. $query->fields('td');
  125. $query->fields('tv', array('machine_name'));
  126. $query->condition('td.tid', $test);
  127. $result = $query->execute();
  128. foreach ($result as $term) {
  129. if ($vocabularies && empty($vocabularies[$term->machine_name])) {
  130. $validated_cache[$term->tid] = FALSE;
  131. return FALSE;
  132. }
  133. $term = taxonomy_term_load($term->tid);
  134. $titles[] = $validated_cache[$term->tid] = check_plain(entity_label('taxonomy_term', $term));
  135. unset($test[$term->tid]);
  136. }
  137. }
  138. // Remove duplicate titles
  139. $titles = array_unique($titles);
  140. $this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles);
  141. // If this is not empty, we did not find a tid.
  142. return empty($test);
  143. case 'name':
  144. case 'convert':
  145. $query = db_select('taxonomy_term_data', 'td');
  146. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  147. $query->fields('td');
  148. $query->fields('tv', array('machine_name'));
  149. if (!empty($vocabularies)) {
  150. $query->condition('tv.machine_name', $vocabularies);
  151. }
  152. if ($transform) {
  153. $query->where("replace(td.name, ' ', '-') = :name", array(':name' => $argument));
  154. }
  155. else {
  156. $query->condition('td.name', $argument);
  157. }
  158. $term = $query->execute()->fetchObject();
  159. if ($term && (empty($vocabularies) || !empty($vocabularies[$term->machine_name]))) {
  160. if ($type == 'convert') {
  161. $this->argument->argument = $term->tid;
  162. }
  163. $term = taxonomy_term_load($term->tid);
  164. $this->argument->validated_title = check_plain(entity_label('taxonomy_term', $term));
  165. return TRUE;
  166. }
  167. return FALSE;
  168. }
  169. }
  170. function process_summary_arguments(&$args) {
  171. $type = $this->options['type'];
  172. $transform = $this->options['transform'];
  173. $vocabularies = array_filter($this->options['vocabularies']);
  174. if ($type == 'convert') {
  175. $arg_keys = array_flip($args);
  176. $query = db_select('taxonomy_term_data', 'td');
  177. $query->condition('tid', $args);
  178. $query->addField('td', 'tid', 'tid');
  179. if (!empty($vocabularies)) {
  180. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  181. $query->condition('tv.machine_name', $vocabularies);
  182. }
  183. if ($transform) {
  184. $query->addExpression("REPLACE(td.name, ' ', '-')", 'name');
  185. }
  186. else {
  187. $query->addField('td', 'name', 'name');
  188. }
  189. foreach ($query->execute()->fetchAllKeyed() as $tid => $term) {
  190. $args[$arg_keys[$tid]] = $term;
  191. }
  192. }
  193. }
  194. }