materio-d9/web/modules/custom/materio_sapi/materio_sapi.module

130 lines
4.5 KiB
PHP

<?php
/**
* @file
* Contains materio_sapi.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Solarium\QueryType\Select\Query\Query;
use Drupal\search_api\Query\QueryInterface;
use Drupal\Core\Template\Attribute;
/**
* Implements hook_help().
*/
function materio_sapi_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the materio_sapi module.
case 'help.page.materio_sapi':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Search Api Materio module') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_search_api_solr_query_alter();
*/
function materio_sapi_search_api_solr_query_alter(Query $solarium_query, QueryInterface $query) {
if ($termid = (int)$query->getOption('termid')) {
// get solarium fileds name
$index = $query->getIndex();
$solrFields = $index->getServerInstance()
->getBackend()
->getSolrFieldNames($index);
// tag_tid"itm_tag_tid"
// thesaurus_tid"itm_thesaurus_tid"
$tag_fname = $solrFields['tag_tid'];
$thes_fname = $solrFields['thesaurus_tid'];
// $solarium_query->addParam('bf', "recip(abs(ms(NOW,{$solrField})),3.16e-11,10,0.1)");
// $bfparam = "if(or(gt(termfreq({$tag_fname},{$termid}),0),gt(termfreq({$thes_fname},{$termid}),0)),^21,0)";
$bfparam = "if(or(exists(query({$tag_fname}:{$termid})),exists(query({$thes_fname}:{$termid}))),^21,0)";
// boost=if(or(exists(query(itm_tag_tid:396)),exists(query(itm_thesaurus_tid:396))),^21,0 )
$solarium_query->addParam('boost', $bfparam);
// $solarium_query->addParam('debugQuery', 'on');
}
}
/**
* Prepares variables for fieldset element templates.
*
* Default template: fieldset.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #attributes, #children, #description, #id, #title,
* #value.
*/
function materio_sapi_preprocess_fieldset(&$variables) {
if (isset($variables['element']['#legend_attributes'])) {
$variables['legend']['attributes'] = new Attribute($variables['element']['#legend_attributes']);
}
}
// ? https://happyculture.coop/blog/drupal-8-declarer-un-champ-extrafield-calcule-computed-field
// ? https://fivejars.com/blog/entity-basefielddefinitions-fields-examples-drupal-8
/**
* Implements hook_entity_bundle_field_info().
*/
function materio_sapi_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
$fields = [];
if ($entity_type->id() == 'node') {
if ($bundle == 'materiau') {
for ($i=0; $i < 5 ; $i++) {
$fields["thesaurus_$i"] = \Drupal\Core\Field\BaseFieldDefinition::create('entity_reference')
->setLabel(t("Thesaurus $i"))
->setComputed(TRUE)
->setSetting('target_type', 'taxonomy_term')
->setSetting('handler_settings',['target_bundles'=>['thesaurus'=>'thesaurus']] )
->setClass('\Drupal\materio_sapi\ThesaurusRefComputed')
->setDisplayConfigurable('view', FALSE);
}
$fields["field_country_name_computed"] = \Drupal\Core\Field\BaseFieldDefinition::create('string')
->setLabel(t("Country Name"))
->setComputed(TRUE)
->setTranslatable(TRUE)
->setClass('\Drupal\materio_sapi\CountryNameNodeComputed')
->setDisplayConfigurable('view', FALSE);
}
}
// if ($entity_type->id() == 'taxonomy_term') {
// if ($bundle == 'company') {
// $fields["field_country_name_computed"] = \Drupal\Core\Field\BaseFieldDefinition::create('string')
// ->setName('field_country_name_computed')
// ->setLabel(t("Country Name"))
// ->setComputed(TRUE)
// ->setTranslatable(TRUE)
// ->setClass('\Drupal\materio_sapi\CountryNameComputed')
// ->setDisplayConfigurable('view', FALSE);
// }
// }
return $fields;
}
// ? https://www.webomelette.com/creating-pseudo-fields-drupal-8
// /**
// * Implements hook_entity_extra_field_info().
// */
// function materio_sapi_entity_extra_field_info() {
// $extra = array();
// foreach (NodeType::loadMultiple() as $bundle) {
// $extra['node'][$bundle->Id()]['display']['my_own_pseudo_field'] = array(
// 'label' => t('My own field'),
// 'description' => t('This is my own pseudo-field'),
// 'weight' => 100,
// 'visible' => false,
// );
// }
// return $extra;
// }