materio_sapi.module 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * @file
  4. * Contains materio_sapi.module.
  5. */
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. use Solarium\QueryType\Select\Query\Query;
  8. use Drupal\search_api\Query\QueryInterface;
  9. use Drupal\Core\Template\Attribute;
  10. /**
  11. * Implements hook_help().
  12. */
  13. function materio_sapi_help($route_name, RouteMatchInterface $route_match) {
  14. switch ($route_name) {
  15. // Main module help for the materio_sapi module.
  16. case 'help.page.materio_sapi':
  17. $output = '';
  18. $output .= '<h3>' . t('About') . '</h3>';
  19. $output .= '<p>' . t('Search Api Materio module') . '</p>';
  20. return $output;
  21. default:
  22. }
  23. }
  24. /**
  25. * Implements hook_search_api_solr_query_alter();
  26. */
  27. function materio_sapi_search_api_solr_query_alter(Query $solarium_query, QueryInterface $query) {
  28. if ($termid = (int)$query->getOption('termid')) {
  29. // get solarium fileds name
  30. $index = $query->getIndex();
  31. $solrFields = $index->getServerInstance()
  32. ->getBackend()
  33. ->getSolrFieldNames($index);
  34. // tag_tid"itm_tag_tid"
  35. // thesaurus_tid"itm_thesaurus_tid"
  36. $tag_fname = $solrFields['tag_tid'];
  37. $thes_fname = $solrFields['thesaurus_tid'];
  38. // $solarium_query->addParam('bf', "recip(abs(ms(NOW,{$solrField})),3.16e-11,10,0.1)");
  39. // $bfparam = "if(or(gt(termfreq({$tag_fname},{$termid}),0),gt(termfreq({$thes_fname},{$termid}),0)),^21,0)";
  40. $bfparam = "if(or(exists(query({$tag_fname}:{$termid})),exists(query({$thes_fname}:{$termid}))),^21,0)";
  41. // boost=if(or(exists(query(itm_tag_tid:396)),exists(query(itm_thesaurus_tid:396))),^21,0 )
  42. $solarium_query->addParam('boost', $bfparam);
  43. // $solarium_query->addParam('debugQuery', 'on');
  44. }
  45. }
  46. /**
  47. * Prepares variables for fieldset element templates.
  48. *
  49. * Default template: fieldset.html.twig.
  50. *
  51. * @param array $variables
  52. * An associative array containing:
  53. * - element: An associative array containing the properties of the element.
  54. * Properties used: #attributes, #children, #description, #id, #title,
  55. * #value.
  56. */
  57. function materio_sapi_preprocess_fieldset(&$variables) {
  58. if (isset($variables['element']['#legend_attributes'])) {
  59. $variables['legend']['attributes'] = new Attribute($variables['element']['#legend_attributes']);
  60. }
  61. }