handler_argument_more_like_this.inc 2.5 KB

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