handler_argument_more_like_this.inc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Views argument handler providing a list of related items for search servers
  4. * supporting the "search_api_mlt" feature.
  5. */
  6. class SearchApiViewsHandlerArgumentMoreLikeThis extends SearchApiViewsHandlerArgument {
  7. /**
  8. * Specify the options this filter uses.
  9. */
  10. public function option_definition() {
  11. $options = parent::option_definition();
  12. unset($options['break_phrase']);
  13. $options['fields'] = array('default' => array());
  14. return $options;
  15. }
  16. /**
  17. * Extend the options form a bit.
  18. */
  19. public function options_form(&$form, &$form_state) {
  20. parent::options_form($form, $form_state);
  21. unset($form['break_phrase']);
  22. $index = search_api_index_load(substr($this->table, 17));
  23. if (!empty($index->options['fields'])) {
  24. $fields = array();
  25. foreach ($index->getFields() as $key => $field) {
  26. $fields[$key] = $field['name'];
  27. }
  28. }
  29. if (!empty($fields)) {
  30. $form['fields'] = array(
  31. '#type' => 'select',
  32. '#title' => t('Fields for Similarity'),
  33. '#description' => t('Select the fields that will be used for finding similar content. If no fields are selected, all available fields will be used.'),
  34. '#options' => $fields,
  35. '#size' => min(8, count($fields)),
  36. '#multiple' => TRUE,
  37. '#default_value' => $this->options['fields'],
  38. );
  39. }
  40. else {
  41. $form['fields'] = array(
  42. '#type' => 'value',
  43. '#value' => array(),
  44. );
  45. }
  46. }
  47. /**
  48. * Set up the query for this argument.
  49. *
  50. * The argument sent may be found at $this->argument.
  51. */
  52. public function query($group_by = FALSE) {
  53. $server = $this->query->getIndex()->server();
  54. if (!$server->supportsFeature('search_api_mlt')) {
  55. $class = search_api_get_service_info($server->class);
  56. throw new SearchApiException(t('The search service "@class" does not offer "More like this" functionality.',
  57. array('@class' => $class['name'])));
  58. return;
  59. }
  60. $fields = $this->options['fields'] ? $this->options['fields'] : array();
  61. if (empty($fields)) {
  62. foreach ($this->query->getIndex()->options['fields'] as $key => $field) {
  63. $fields[] = $key;
  64. }
  65. }
  66. $mlt = array(
  67. 'id' => $this->argument,
  68. 'fields' => $fields,
  69. );
  70. $this->query->getSearchApiQuery()->setOption('search_api_mlt', $mlt);
  71. }
  72. }