added country name to sapi index
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
29
web/modules/custom/materio_sapi/src/CountryNameComputed.php
Normal file
29
web/modules/custom/materio_sapi/src/CountryNameComputed.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\materio_sapi;
|
||||
|
||||
use Drupal\Core\Field\FieldItemList;
|
||||
use Drupal\Core\TypedData\ComputedItemListTrait;
|
||||
|
||||
class CountryNameComputed extends FieldItemList {
|
||||
use ComputedItemListTrait;
|
||||
|
||||
protected function computeValue() {
|
||||
$term = $this->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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\materio_sapi;
|
||||
|
||||
use Drupal\Core\Field\FieldItemList;
|
||||
use Drupal\Core\TypedData\ComputedItemListTrait;
|
||||
// use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
|
||||
class CountryNameNodeComputed extends FieldItemList {
|
||||
use ComputedItemListTrait;
|
||||
|
||||
protected function computeValue() {
|
||||
$node = $this->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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user