From 14f2e4a9aa14cf957c174309db31885c85298147 Mon Sep 17 00:00:00 2001 From: bach Date: Mon, 11 Oct 2021 21:35:29 +0200 Subject: [PATCH] added country name to sapi index --- config/sync/search_api.index.database.yml | 31 ++++--------- .../custom/materio_sapi/materio_sapi.module | 20 ++++++++- .../materio_sapi/src/CountryNameComputed.php | 29 +++++++++++++ .../src/CountryNameNodeComputed.php | 43 +++++++++++++++++++ 4 files changed, 99 insertions(+), 24 deletions(-) create mode 100644 web/modules/custom/materio_sapi/src/CountryNameComputed.php create mode 100644 web/modules/custom/materio_sapi/src/CountryNameNodeComputed.php diff --git a/config/sync/search_api.index.database.yml b/config/sync/search_api.index.database.yml index 9652b60..aa1da16 100644 --- a/config/sync/search_api.index.database.yml +++ b/config/sync/search_api.index.database.yml @@ -4,16 +4,15 @@ status: true dependencies: module: - search_api_solr + - materio_sapi - taxonomy - node - - materio_sapi - synonyms - search_api - address config: - field.storage.node.body - field.storage.node.field_distributor - - field.storage.taxonomy_term.field_public_address - field.storage.node.field_famille - field.storage.node.field_manufacturer - field.storage.node.field_reference @@ -68,28 +67,15 @@ name: database description: '' read_only: false field_settings: - country_code: - label: 'Manufacturer » Terme de taxonomie » Address » The two-letter country code' + field_country_name_computed: + label: 'Country Name' datasource_id: 'entity:node' - property_path: 'field_manufacturer:entity:field_public_address:country_code' - type: solr_text_wstoken + property_path: field_country_name_computed + type: 'solr_text_custom:edgeasciistring' + boost: !!float 3 dependencies: - config: - - field.storage.node.field_manufacturer - - field.storage.taxonomy_term.field_public_address module: - - taxonomy - country_code_1: - label: 'Distributor » Terme de taxonomie » Address » The two-letter country code' - datasource_id: 'entity:node' - property_path: 'field_distributor:entity:field_public_address:country_code' - type: solr_text_wstoken - dependencies: - config: - - field.storage.node.field_distributor - - field.storage.taxonomy_term.field_public_address - module: - - taxonomy + - materio_sapi field_famille: label: Famille datasource_id: 'entity:node' @@ -428,8 +414,7 @@ processor_settings: html_filter: all_fields: true fields: - - country_code - - country_code_1 + - field_country_name_computed - field_famille - field_reference - field_short_description diff --git a/web/modules/custom/materio_sapi/materio_sapi.module b/web/modules/custom/materio_sapi/materio_sapi.module index f8caa18..9c33706 100644 --- a/web/modules/custom/materio_sapi/materio_sapi.module +++ b/web/modules/custom/materio_sapi/materio_sapi.module @@ -70,6 +70,7 @@ function materio_sapi_preprocess_fieldset(&$variables) { // ? 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(). */ @@ -86,8 +87,25 @@ function materio_sapi_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInt ->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; } diff --git a/web/modules/custom/materio_sapi/src/CountryNameComputed.php b/web/modules/custom/materio_sapi/src/CountryNameComputed.php new file mode 100644 index 0000000..4b06c9e --- /dev/null +++ b/web/modules/custom/materio_sapi/src/CountryNameComputed.php @@ -0,0 +1,29 @@ +getEntity(); + $name = $term->getName(); + $address = $term->get('field_public_address')->first(); + if ($address) { + $country_code = $address->getCountryCode(); + if ($country_code) { + $full_country_list = \Drupal::service('country_manager')->getList(); + $country_name = $full_country_list[$country_code]; + $lang = \Drupal::languageManager()->getCurrentLanguage()->getId(); + $translated_markup = $country_name->render(); + if($parent->id() == 5719){ + $t="t"; + } + $this->list[0] = $this->createItem(0, $translated_markup); + } + } + } +} diff --git a/web/modules/custom/materio_sapi/src/CountryNameNodeComputed.php b/web/modules/custom/materio_sapi/src/CountryNameNodeComputed.php new file mode 100644 index 0000000..be5f12d --- /dev/null +++ b/web/modules/custom/materio_sapi/src/CountryNameNodeComputed.php @@ -0,0 +1,43 @@ +getEntity(); + $node_lang_code = $node->language()->getId(); + // $distrib = $node->get('field_distributor')->referencedEntities(); + // $manuf = $node->get('field_manufacturer')->referencedEntities(); + $companies = array_merge($node->get('field_distributor')->referencedEntities(), $node->get('field_manufacturer')->referencedEntities()); + $country_codes = []; + foreach ($companies as $term) { + $address = $term->get('field_public_address')->first(); + if ($address) { + $country_code = $address->getCountryCode(); + if (!in_array($country_code, $country_codes)){ + $country_codes[] = $country_code; + } + } + } + if (count($country_codes)) { + $full_country_list = \Drupal::service('country_manager')->getList(); + foreach ($country_codes as $i => $country_code) { + /** Drupal\Core\StringTranslation\TranslatableMarkup **/ + $country_name = $full_country_list[$country_code]; + // $country_name->options['langcode'] = $this->langcode; + // $lang = \Drupal::languageManager()->getCurrentLanguage()->getId(); + $untranslated = $country_name->getUntranslatedString(); + // $translated = $country_name->render(['langcode' => $node_lang_code]); + $translated = t($untranslated, [], ['langcode'=>$node_lang_code]); + // $this->list[$i] = $this->createItem($i, "lang: $lang, node:$node_lang_code, $untranslated, $translated"); + $this->list[$i] = $this->createItem($i, $translated); + } + } + } +}