From 7056bce8033de230099a70e116a5cbba9cde7872 Mon Sep 17 00:00:00 2001 From: bach Date: Fri, 8 Oct 2021 13:39:17 +0200 Subject: [PATCH] added thesaurus ordered in sapi solr database index --- config/devel/devel.settings.yml | 2 +- config/sync/search_api.index.database.yml | 131 +++++++++++++++++- ...d_type.text_edgestring_ascii_und_6_0_0.yml | 2 +- .../custom/materio_sapi/materio_sapi.module | 43 ++++++ .../materio_sapi/src/ThesaurusRefComputed.php | 47 +++++++ 5 files changed, 218 insertions(+), 7 deletions(-) create mode 100644 web/modules/custom/materio_sapi/src/ThesaurusRefComputed.php diff --git a/config/devel/devel.settings.yml b/config/devel/devel.settings.yml index 228d3eb4..743fcf6b 100644 --- a/config/devel/devel.settings.yml +++ b/config/devel/devel.settings.yml @@ -3,6 +3,6 @@ raw_names: false error_handlers: - 0 rebuild_theme: false -devel_dumper: default +devel_dumper: kint debug_logfile: 'temporary://drupal_debug.txt' debug_pre: false diff --git a/config/sync/search_api.index.database.yml b/config/sync/search_api.index.database.yml index 14aa1e84..8c9f7d2e 100644 --- a/config/sync/search_api.index.database.yml +++ b/config/sync/search_api.index.database.yml @@ -6,6 +6,8 @@ dependencies: - search_api_solr - taxonomy - node + - materio_sapi + - synonyms - search_api - address config: @@ -204,25 +206,134 @@ field_settings: label: 'Thesaurus » Taxonomy term » Name' datasource_id: 'entity:node' property_path: 'field_thesaurus:entity:name' - type: solr_text_wstoken - boost: !!float 8 + type: 'solr_text_custom:edgeasciistring' + boost: !!float 2 dependencies: config: - field.storage.node.field_thesaurus module: - taxonomy + thesaurus_name_0: + label: 'Thesaurus 0 » Terme de taxonomie » Nom' + datasource_id: 'entity:node' + property_path: 'thesaurus_0:entity:name' + type: 'solr_text_custom:edgeasciistring' + boost: !!float 21 + dependencies: + module: + - materio_sapi + - taxonomy + thesaurus_name_1: + label: 'Thesaurus 1 » Terme de taxonomie » Nom' + datasource_id: 'entity:node' + property_path: 'thesaurus_1:entity:name' + type: 'solr_text_custom:edgeasciistring' + boost: !!float 13 + dependencies: + module: + - materio_sapi + - taxonomy + thesaurus_name_2: + label: 'Thesaurus 2 » Terme de taxonomie » Nom' + datasource_id: 'entity:node' + property_path: 'thesaurus_2:entity:name' + type: 'solr_text_custom:edgeasciistring' + boost: !!float 8 + dependencies: + module: + - materio_sapi + - taxonomy + thesaurus_name_3: + label: 'Thesaurus 3 » Terme de taxonomie » Nom' + datasource_id: 'entity:node' + property_path: 'thesaurus_3:entity:name' + type: 'solr_text_custom:edgeasciistring' + boost: !!float 5 + dependencies: + module: + - materio_sapi + - taxonomy + thesaurus_name_4: + label: 'Thesaurus 4 » Terme de taxonomie » Nom' + datasource_id: 'entity:node' + property_path: 'thesaurus_4:entity:name' + type: 'solr_text_custom:edgeasciistring' + boost: !!float 3 + dependencies: + module: + - materio_sapi + - taxonomy thesaurus_synonyms: label: 'Thesaurus » Taxonomy term » Synonyms' datasource_id: 'entity:node' property_path: 'field_thesaurus:entity:field_synonyms' - type: solr_text_wstoken - boost: !!float 8 + type: 'solr_text_custom:edgeasciistring' + boost: !!float 2 dependencies: config: - field.storage.node.field_thesaurus - field.storage.taxonomy_term.field_synonyms module: - taxonomy + thesaurus_synonyms_0: + label: 'Thesaurus 0 » Terme de taxonomie » Synonyms list' + datasource_id: 'entity:node' + property_path: 'thesaurus_0:entity:synonyms' + type: 'solr_text_custom:edgeasciistring' + boost: !!float 21 + dependencies: + module: + - materio_sapi + - synonyms + - taxonomy + thesaurus_synonyms_1: + label: 'Thesaurus 1 » Terme de taxonomie » Synonyms' + datasource_id: 'entity:node' + property_path: 'thesaurus_1:entity:field_synonyms' + type: 'solr_text_custom:edgeasciistring' + boost: !!float 13 + dependencies: + config: + - field.storage.taxonomy_term.field_synonyms + module: + - materio_sapi + - taxonomy + thesaurus_synonyms_2: + label: 'Thesaurus 2 » Terme de taxonomie » Synonyms' + datasource_id: 'entity:node' + property_path: 'thesaurus_2:entity:field_synonyms' + type: 'solr_text_custom:edgeasciistring' + boost: !!float 8 + dependencies: + config: + - field.storage.taxonomy_term.field_synonyms + module: + - materio_sapi + - taxonomy + thesaurus_synonyms_3: + label: 'Thesaurus 3 » Terme de taxonomie » Synonyms' + datasource_id: 'entity:node' + property_path: 'thesaurus_3:entity:field_synonyms' + type: 'solr_text_custom:edgeasciistring' + boost: !!float 5 + dependencies: + config: + - field.storage.taxonomy_term.field_synonyms + module: + - materio_sapi + - taxonomy + thesaurus_synonyms_4: + label: 'Thesaurus 4 » Terme de taxonomie » Synonyms' + datasource_id: 'entity:node' + property_path: 'thesaurus_4:entity:field_synonyms' + type: 'solr_text_custom:edgeasciistring' + boost: !!float 3 + dependencies: + config: + - field.storage.taxonomy_term.field_synonyms + module: + - materio_sapi + - taxonomy thesaurus_tid: label: 'Thesaurus » Taxonomy term » Term ID' datasource_id: 'entity:node' @@ -239,7 +350,7 @@ field_settings: datasource_id: 'entity:node' property_path: title type: text - boost: !!float 13 + boost: !!float 21 dependencies: module: - node @@ -328,7 +439,17 @@ processor_settings: - tag_name - tag_synonyms - thesaurus_name + - thesaurus_name_0 + - thesaurus_name_1 + - thesaurus_name_2 + - thesaurus_name_3 + - thesaurus_name_4 - thesaurus_synonyms + - thesaurus_synonyms_0 + - thesaurus_synonyms_1 + - thesaurus_synonyms_2 + - thesaurus_synonyms_3 + - thesaurus_synonyms_4 - title - type - uuid diff --git a/config/sync/search_api_solr.solr_field_type.text_edgestring_ascii_und_6_0_0.yml b/config/sync/search_api_solr.solr_field_type.text_edgestring_ascii_und_6_0_0.yml index 32539dcb..a8a32b99 100644 --- a/config/sync/search_api_solr.solr_field_type.text_edgestring_ascii_und_6_0_0.yml +++ b/config/sync/search_api_solr.solr_field_type.text_edgestring_ascii_und_6_0_0.yml @@ -1,4 +1,4 @@ -uuid: af25836c-6eca-4621-a803-7a7394fff660 +uuid: dafb8d3b-f321-4b97-b412-40e1e6b66b25 langcode: en status: true dependencies: { } diff --git a/web/modules/custom/materio_sapi/materio_sapi.module b/web/modules/custom/materio_sapi/materio_sapi.module index bec30498..f8caa189 100644 --- a/web/modules/custom/materio_sapi/materio_sapi.module +++ b/web/modules/custom/materio_sapi/materio_sapi.module @@ -67,3 +67,46 @@ function materio_sapi_preprocess_fieldset(&$variables) { $variables['legend']['attributes'] = new Attribute($variables['element']['#legend_attributes']); } } + + +// ? https://happyculture.coop/blog/drupal-8-declarer-un-champ-extrafield-calcule-computed-field +/** + * 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); + } + } + } + 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; +// } \ No newline at end of file diff --git a/web/modules/custom/materio_sapi/src/ThesaurusRefComputed.php b/web/modules/custom/materio_sapi/src/ThesaurusRefComputed.php new file mode 100644 index 00000000..dd2a1724 --- /dev/null +++ b/web/modules/custom/materio_sapi/src/ThesaurusRefComputed.php @@ -0,0 +1,47 @@ +getName(); + switch ($field_name) { + case 'thesaurus_0': + $delta = 0; + break; + case 'thesaurus_1': + $delta = 1; + break; + case 'thesaurus_2': + $delta = 2; + break; + case 'thesaurus_3': + $delta = 3; + break; + case 'thesaurus_4': + $delta = 4; + break; + default: + $delta = -1; + break; + } + + $thes_terms = $this->getParent()->getValue()->get('field_thesaurus')->getValue(); + + if (count($thes_terms) > 0) { + if (isset($thes_terms[$delta])) { + $term = $thes_terms[$delta]; + $this->list[0] = $this->createItem(0, $term); + $t="t"; + } + } + } +}