handler_filter_taxonomy_term.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * @file
  4. * Contains SearchApiViewsHandlerFilterTaxonomyTerm.
  5. */
  6. /**
  7. * Views filter handler class for taxonomy term entities.
  8. *
  9. * Based on views_handler_filter_term_node_tid.
  10. */
  11. class SearchApiViewsHandlerFilterTaxonomyTerm extends SearchApiViewsHandlerFilterEntity {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function has_extra_options() {
  16. return !empty($this->definition['vocabulary']);
  17. }
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function option_definition() {
  22. $options = parent::option_definition();
  23. $options['type'] = array('default' => !empty($this->definition['vocabulary']) ? 'textfield' : 'select');
  24. $options['hierarchy'] = array('default' => 0);
  25. $options['expose']['contains']['reduce'] = array('default' => FALSE);
  26. $options['error_message'] = array('default' => TRUE, 'bool' => TRUE);
  27. return $options;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function extra_options_form(&$form, &$form_state) {
  33. $form['type'] = array(
  34. '#type' => 'radios',
  35. '#title' => t('Selection type'),
  36. '#options' => array('select' => t('Dropdown'), 'textfield' => t('Autocomplete')),
  37. '#default_value' => $this->options['type'],
  38. );
  39. $form['hierarchy'] = array(
  40. '#type' => 'checkbox',
  41. '#title' => t('Show hierarchy in dropdown'),
  42. '#default_value' => !empty($this->options['hierarchy']),
  43. );
  44. $form['hierarchy']['#states']['visible'][':input[name="options[type]"]']['value'] = 'select';
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function value_form(&$form, &$form_state) {
  50. parent::value_form($form, $form_state);
  51. if (!empty($this->definition['vocabulary'])) {
  52. $vocabulary = taxonomy_vocabulary_machine_name_load($this->definition['vocabulary']);
  53. $title = t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->name));
  54. }
  55. else {
  56. $vocabulary = FALSE;
  57. $title = t('Select terms');
  58. }
  59. $form['value']['#title'] = $title;
  60. if ($vocabulary && $this->options['type'] == 'textfield') {
  61. $form['value']['#autocomplete_path'] = 'admin/views/ajax/autocomplete/taxonomy/' . $vocabulary->vid;
  62. }
  63. else {
  64. if ($vocabulary && !empty($this->options['hierarchy'])) {
  65. $tree = taxonomy_get_tree($vocabulary->vid, 0, NULL, TRUE);
  66. $options = array();
  67. if ($tree) {
  68. foreach ($tree as $term) {
  69. $choice = new stdClass();
  70. $choice->option = array($term->tid => str_repeat('-', $term->depth) . check_plain(entity_label('taxonomy_term', $term)));
  71. $options[] = $choice;
  72. }
  73. }
  74. }
  75. else {
  76. $options = array();
  77. $query = db_select('taxonomy_term_data', 'td');
  78. $query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  79. $query->fields('td');
  80. $query->orderby('tv.weight');
  81. $query->orderby('tv.name');
  82. $query->orderby('td.weight');
  83. $query->orderby('td.name');
  84. $query->addTag('taxonomy_term_access');
  85. if ($vocabulary) {
  86. $query->condition('tv.machine_name', $vocabulary->machine_name);
  87. }
  88. $result = $query->execute();
  89. $tids = array();
  90. foreach ($result as $term) {
  91. $tids[] = $term->tid;
  92. }
  93. $terms = taxonomy_term_load_multiple($tids);
  94. foreach ($terms as $term) {
  95. $options[$term->tid] = check_plain(entity_label('taxonomy_term', $term));
  96. }
  97. }
  98. $default_value = (array) $this->value;
  99. if (!empty($form_state['exposed'])) {
  100. $identifier = $this->options['expose']['identifier'];
  101. if (!empty($this->options['expose']['reduce'])) {
  102. $options = $this->reduce_value_options($options);
  103. if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
  104. $default_value = array();
  105. }
  106. }
  107. if (empty($this->options['expose']['multiple'])) {
  108. if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
  109. $default_value = 'All';
  110. }
  111. elseif (empty($default_value)) {
  112. $keys = array_keys($options);
  113. $default_value = array_shift($keys);
  114. }
  115. // Due to #1464174 there is a chance that array('') was saved in the
  116. // admin ui. Let's choose a safe default value.
  117. elseif ($default_value == array('')) {
  118. $default_value = 'All';
  119. }
  120. else {
  121. $copy = $default_value;
  122. $default_value = array_shift($copy);
  123. }
  124. }
  125. }
  126. $form['value']['#type'] = 'select';
  127. $form['value']['#multiple'] = TRUE;
  128. $form['value']['#options'] = $options;
  129. $form['value']['#size'] = min(9, count($options));
  130. $form['value']['#default_value'] = $default_value;
  131. if (!empty($form_state['exposed']) && isset($identifier) && !isset($form_state['input'][$identifier])) {
  132. $form_state['input'][$identifier] = $default_value;
  133. }
  134. }
  135. }
  136. /**
  137. * Reduces the available exposed options according to the selection.
  138. */
  139. protected function reduce_value_options(array $options) {
  140. foreach ($options as $id => $option) {
  141. if (empty($this->options['value'][$id])) {
  142. unset($options[$id]);
  143. }
  144. }
  145. return $options;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function value_validate($form, &$form_state) {
  151. // We only validate if they've chosen the text field style.
  152. if ($this->options['type'] != 'textfield') {
  153. return;
  154. }
  155. parent::value_validate($form, $form_state);
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function accept_exposed_input($input) {
  161. if (empty($this->options['exposed'])) {
  162. return TRUE;
  163. }
  164. // We need to know the operator, which is normally set in
  165. // views_handler_filter::accept_exposed_input(), before we actually call
  166. // the parent version of ourselves.
  167. if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
  168. $this->operator = $input[$this->options['expose']['operator_id']];
  169. }
  170. // If view is an attachment and is inheriting exposed filters, then assume
  171. // exposed input has already been validated.
  172. if (!empty($this->view->is_attachment) && $this->view->display_handler->uses_exposed()) {
  173. $this->validated_exposed_input = (array) $this->view->exposed_raw_input[$this->options['expose']['identifier']];
  174. }
  175. // If we're checking for EMPTY or NOT, we don't need any input, and we can
  176. // say that our input conditions are met by just having the right operator.
  177. if ($this->operator == 'empty' || $this->operator == 'not empty') {
  178. return TRUE;
  179. }
  180. // If it's non-required and there's no value don't bother filtering.
  181. if (!$this->options['expose']['required'] && empty($this->validated_exposed_input)) {
  182. return FALSE;
  183. }
  184. return parent::accept_exposed_input($input);
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function exposed_validate(&$form, &$form_state) {
  190. if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) {
  191. return;
  192. }
  193. // We only validate if they've chosen the text field style.
  194. if ($this->options['type'] != 'textfield') {
  195. $input = $form_state['values'][$this->options['expose']['identifier']];
  196. if ($this->options['is_grouped'] && isset($this->options['group_info']['group_items'][$input])) {
  197. $input = $this->options['group_info']['group_items'][$input]['value'];
  198. }
  199. if ($input != 'All') {
  200. $this->validated_exposed_input = (array) $input;
  201. }
  202. return;
  203. }
  204. parent::exposed_validate($form, $form_state);
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function expose_options() {
  210. parent::expose_options();
  211. $this->options['expose']['reduce'] = FALSE;
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. protected function validate_entity_strings(array &$form, array $values) {
  217. if (empty($values)) {
  218. return array();
  219. }
  220. $tids = array();
  221. $names = array();
  222. $missing = array();
  223. foreach ($values as $value) {
  224. $missing[strtolower($value)] = TRUE;
  225. $names[] = $value;
  226. }
  227. if (!$names) {
  228. return FALSE;
  229. }
  230. $query = db_select('taxonomy_term_data', 'td');
  231. $query->innerJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  232. $query->fields('td');
  233. $query->condition('td.name', $names);
  234. if (!empty($this->definition['vocabulary'])) {
  235. $query->condition('tv.machine_name', $this->definition['vocabulary']);
  236. }
  237. $query->addTag('taxonomy_term_access');
  238. $result = $query->execute();
  239. foreach ($result as $term) {
  240. unset($missing[strtolower($term->name)]);
  241. $tids[] = $term->tid;
  242. }
  243. if ($missing) {
  244. if (!empty($this->options['error_message'])) {
  245. form_error($form, format_plural(count($missing), 'Unable to find term: @terms', 'Unable to find terms: @terms', array('@terms' => implode(', ', array_keys($missing)))));
  246. }
  247. else {
  248. // Add a bogus TID which will show an empty result for a positive filter
  249. // and be ignored for an excluding one.
  250. $tids[] = 0;
  251. }
  252. }
  253. return $tids;
  254. }
  255. /**
  256. * {@inheritdoc}
  257. */
  258. public function expose_form(&$form, &$form_state) {
  259. parent::expose_form($form, $form_state);
  260. if ($this->options['type'] == 'select') {
  261. $form['expose']['reduce'] = array(
  262. '#type' => 'checkbox',
  263. '#title' => t('Limit list to selected items'),
  264. '#description' => t('If checked, the only items presented to the user will be the ones selected here.'),
  265. '#default_value' => $this->options['expose']['reduce'],
  266. );
  267. }
  268. else {
  269. $form['error_message'] = array(
  270. '#type' => 'checkbox',
  271. '#title' => t('Display error message'),
  272. '#description' => t('Display an error message if one of the entered terms could not be found.'),
  273. '#default_value' => $this->options['error_message'],
  274. );
  275. }
  276. }
  277. /**
  278. * {@inheritdoc}
  279. */
  280. protected function ids_to_strings(array $ids) {
  281. $ids = array_filter($ids);
  282. if (!$ids) {
  283. return '';
  284. }
  285. return implode(', ', db_select('taxonomy_term_data', 'td')
  286. ->fields('td', array('name'))
  287. ->condition('td.tid', $ids)
  288. ->execute()
  289. ->fetchCol());
  290. }
  291. }