materio_sapi.module 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }
  62. // ? https://happyculture.coop/blog/drupal-8-declarer-un-champ-extrafield-calcule-computed-field
  63. /**
  64. * Implements hook_entity_bundle_field_info().
  65. */
  66. function materio_sapi_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
  67. $fields = [];
  68. if ($entity_type->id() == 'node') {
  69. if ($bundle == 'materiau') {
  70. for ($i=0; $i < 5 ; $i++) {
  71. $fields["thesaurus_$i"] = \Drupal\Core\Field\BaseFieldDefinition::create('entity_reference')
  72. ->setLabel(t("Thesaurus $i"))
  73. ->setComputed(TRUE)
  74. ->setSetting('target_type', 'taxonomy_term')
  75. ->setSetting('handler_settings',['target_bundles'=>['thesaurus'=>'thesaurus']] )
  76. ->setClass('\Drupal\materio_sapi\ThesaurusRefComputed')
  77. ->setDisplayConfigurable('view', FALSE);
  78. }
  79. }
  80. }
  81. return $fields;
  82. }
  83. // ? https://www.webomelette.com/creating-pseudo-fields-drupal-8
  84. // /**
  85. // * Implements hook_entity_extra_field_info().
  86. // */
  87. // function materio_sapi_entity_extra_field_info() {
  88. // $extra = array();
  89. // foreach (NodeType::loadMultiple() as $bundle) {
  90. // $extra['node'][$bundle->Id()]['display']['my_own_pseudo_field'] = array(
  91. // 'label' => t('My own field'),
  92. // 'description' => t('This is my own pseudo-field'),
  93. // 'weight' => 100,
  94. // 'visible' => false,
  95. // );
  96. // }
  97. // return $extra;
  98. // }