handler_argument_more_like_this.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. $options['entity_type'] = array('default' => FALSE);
  17. $options['fields'] = array('default' => array());
  18. return $options;
  19. }
  20. /**
  21. * Extend the options form a bit.
  22. */
  23. public function options_form(&$form, &$form_state) {
  24. parent::options_form($form, $form_state);
  25. unset($form['break_phrase']);
  26. unset($form['not']);
  27. $index = search_api_index_load(substr($this->table, 17));
  28. if ($index->datasource() instanceof SearchApiCombinedEntityDataSourceController) {
  29. $types = array_intersect_key(search_api_entity_type_options_list(), array_flip($index->options['datasource']['types']));
  30. $form['entity_type'] = array(
  31. '#type' => 'select',
  32. '#title' => t('Entity type'),
  33. '#description' => t('Select the entity type of the argument.'),
  34. '#options' => $types,
  35. '#default_value' => $this->options['entity_type'],
  36. '#required' => TRUE,
  37. );
  38. }
  39. if (!empty($index->options['fields'])) {
  40. $fields = array();
  41. foreach ($index->getFields() as $key => $field) {
  42. $fields[$key] = $field['name'];
  43. }
  44. }
  45. if (!empty($fields)) {
  46. $form['fields'] = array(
  47. '#type' => 'select',
  48. '#title' => t('Fields for Similarity'),
  49. '#description' => t('Select the fields that will be used for finding similar content. If no fields are selected, all available fields will be used.'),
  50. '#options' => $fields,
  51. '#size' => min(8, count($fields)),
  52. '#multiple' => TRUE,
  53. '#default_value' => $this->options['fields'],
  54. );
  55. }
  56. else {
  57. $form['fields'] = array(
  58. '#type' => 'value',
  59. '#value' => array(),
  60. );
  61. }
  62. }
  63. /**
  64. * Set up the query for this argument.
  65. *
  66. * The argument sent may be found at $this->argument.
  67. */
  68. public function query($group_by = FALSE) {
  69. try {
  70. $server = $this->query->getIndex()->server();
  71. if (!$server->supportsFeature('search_api_mlt')) {
  72. $class = search_api_get_service_info($server->class);
  73. watchdog('search_api_views', 'The search service "@class" does not offer "More like this" functionality.',
  74. array('@class' => $class['name']), WATCHDOG_ERROR);
  75. $this->query->abort();
  76. return;
  77. }
  78. $index_fields = array_keys($this->query->getIndex()->options['fields']);
  79. if (empty($this->options['fields'])) {
  80. $fields = $index_fields;
  81. }
  82. else {
  83. $fields = array_intersect($this->options['fields'], $index_fields);
  84. }
  85. if ($this->query->getIndex()->datasource() instanceof SearchApiCombinedEntityDataSourceController) {
  86. $id = $this->options['entity_type'] . '/' . $this->argument;
  87. }
  88. else {
  89. $id = $this->argument;
  90. }
  91. $mlt = array(
  92. 'id' => $id,
  93. 'fields' => $fields,
  94. );
  95. $this->query->getSearchApiQuery()->setOption('search_api_mlt', $mlt);
  96. }
  97. catch (SearchApiException $e) {
  98. $this->query->abort($e->getMessage());
  99. }
  100. }
  101. }