materio_sapi.module 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // ? https://fivejars.com/blog/entity-basefielddefinitions-fields-examples-drupal-8
  64. /**
  65. * Implements hook_entity_bundle_field_info().
  66. */
  67. function materio_sapi_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
  68. $fields = [];
  69. if ($entity_type->id() == 'node') {
  70. if ($bundle == 'materiau') {
  71. for ($i=0; $i < 5 ; $i++) {
  72. $fields["thesaurus_$i"] = \Drupal\Core\Field\BaseFieldDefinition::create('entity_reference')
  73. ->setLabel(t("Thesaurus $i"))
  74. ->setComputed(TRUE)
  75. ->setSetting('target_type', 'taxonomy_term')
  76. ->setSetting('handler_settings',['target_bundles'=>['thesaurus'=>'thesaurus']] )
  77. ->setClass('\Drupal\materio_sapi\ThesaurusRefComputed')
  78. ->setDisplayConfigurable('view', FALSE);
  79. }
  80. $fields["field_country_name_computed"] = \Drupal\Core\Field\BaseFieldDefinition::create('string')
  81. ->setLabel(t("Country Name"))
  82. ->setComputed(TRUE)
  83. ->setTranslatable(TRUE)
  84. ->setClass('\Drupal\materio_sapi\CountryNameNodeComputed')
  85. ->setDisplayConfigurable('view', FALSE);
  86. }
  87. }
  88. // if ($entity_type->id() == 'taxonomy_term') {
  89. // if ($bundle == 'company') {
  90. // $fields["field_country_name_computed"] = \Drupal\Core\Field\BaseFieldDefinition::create('string')
  91. // ->setName('field_country_name_computed')
  92. // ->setLabel(t("Country Name"))
  93. // ->setComputed(TRUE)
  94. // ->setTranslatable(TRUE)
  95. // ->setClass('\Drupal\materio_sapi\CountryNameComputed')
  96. // ->setDisplayConfigurable('view', FALSE);
  97. // }
  98. // }
  99. return $fields;
  100. }
  101. // ? https://www.webomelette.com/creating-pseudo-fields-drupal-8
  102. // /**
  103. // * Implements hook_entity_extra_field_info().
  104. // */
  105. // function materio_sapi_entity_extra_field_info() {
  106. // $extra = array();
  107. // foreach (NodeType::loadMultiple() as $bundle) {
  108. // $extra['node'][$bundle->Id()]['display']['my_own_pseudo_field'] = array(
  109. // 'label' => t('My own field'),
  110. // 'description' => t('This is my own pseudo-field'),
  111. // 'weight' => 100,
  112. // 'visible' => false,
  113. // );
  114. // }
  115. // return $extra;
  116. // }