synonyms_views_plugin_argument_validate_taxonomy_term.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @file
  4. * An extended version of the 'taxonomy term' argument validator plugin, which
  5. * supports synonyms as arguments.
  6. */
  7. /**
  8. * Validate whether an argument is an acceptable taxonomy term.
  9. */
  10. class synonyms_views_plugin_argument_validate_taxonomy_term extends views_plugin_argument_validate_taxonomy_term {
  11. function options_form(&$form, &$form_state) {
  12. parent::options_form($form, $form_state);
  13. // We just want to add yet another way of validation - synonyms friendly
  14. // validation.
  15. $form['type']['#options']['synonym'] = t('Term name or synonym');
  16. $form['type']['#options']['synonym_tid'] = t('Term name or synonym converted to Term ID');
  17. }
  18. function validate_argument($argument) {
  19. $vocabularies = array_filter($this->options['vocabularies']);
  20. $type = $this->options['type'];
  21. $transform = $this->options['transform'];
  22. switch ($type) {
  23. case 'synonym':
  24. case 'synonym_tid':
  25. // We are requested to do synonyms friendly validation. Firstly we are
  26. // going to query the database trying to find a term with our argument's
  27. // name. If we find one, it is great and we stop right there. Otherwise,
  28. // we look up by synonyms.
  29. $query = db_select('taxonomy_term_data', 't')
  30. ->fields('t', array('tid', 'name'));
  31. if (!empty($vocabularies)) {
  32. $query->innerJoin('taxonomy_vocabulary', 'v', 't.vid = v.vid');
  33. $query->condition('v.machine_name', $vocabularies);
  34. }
  35. if ($transform) {
  36. $query->where("REPLACE(t.name, ' ', '-') = :name", array(':name' => $argument));
  37. }
  38. else {
  39. $query->condition('t.name', $argument, '=');
  40. }
  41. $result = $query->range(0, 1)
  42. ->execute();
  43. if ($result->rowCount()) {
  44. // We have found a term, we are going to use it.
  45. $term = taxonomy_term_load($result->fetchField(0));
  46. $this->argument->argument = $this->synonyms_get_term_property($term);
  47. $this->argument->validated_title = check_plain(entity_label('taxonomy_term', $term));
  48. return TRUE;
  49. }
  50. $bundles = $vocabularies;
  51. if (empty($vocabularies)) {
  52. // At this point we want to convert an empty $vocabularies (implicitly
  53. // meaning "all") into actually a list of all vocabularies.
  54. $bundles = synonyms_bundle_normalize('taxonomy_term', $vocabularies);
  55. }
  56. foreach ($bundles as $bundle) {
  57. $condition = db_and();
  58. if ($transform) {
  59. $condition->where("REPLACE(" . AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER . ", ' ', '-') = :argument", array(
  60. ':argument' => $argument,
  61. ));
  62. }
  63. else {
  64. $condition->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $argument);
  65. }
  66. $synonyms = synonyms_synonyms_find($condition, 'taxonomy_term', $bundle);
  67. if (!empty($synonyms)) {
  68. $term = taxonomy_term_load($synonyms[0]->entity_id);
  69. $this->argument->argument = $this->synonyms_get_term_property($term);
  70. $this->argument->validated_title = check_plain(entity_label('taxonomy_term', $term));
  71. return TRUE;
  72. }
  73. }
  74. // We haven't found any synonym that matched our argument, so there is
  75. // no term to return.
  76. return FALSE;
  77. default:
  78. return parent::validate_argument($argument);
  79. }
  80. }
  81. /**
  82. * Return necessary property (per chosen validator) of encountered term.
  83. *
  84. * @param $term object
  85. * Fully loaded taxonomy term from which necessary property should be
  86. * returned
  87. *
  88. * @return mixed
  89. * Necessary property (per chosen validator) of the provided term
  90. */
  91. function synonyms_get_term_property($term) {
  92. switch ($this->options['type']) {
  93. case 'synonym':
  94. return $term->name;
  95. case 'synonym_tid':
  96. return $term->tid;
  97. }
  98. return NULL;
  99. }
  100. }