浏览代码

translated terms in assisted search

bach 3 年之前
父节点
当前提交
43a8daff8a
共有 1 个文件被更改,包括 9 次插入1 次删除
  1. 9 1
      web/modules/custom/materio_sapi/src/Form/MaterioSapiSearchForm.php

+ 9 - 1
web/modules/custom/materio_sapi/src/Form/MaterioSapiSearchForm.php

@@ -22,6 +22,8 @@ class MaterioSapiSearchForm extends FormBase {
    * {@inheritdoc}
    */
   public function buildForm(array $form, FormStateInterface $form_state) {
+    $lang =  \Drupal::languageManager()->getCurrentLanguage()->getId();
+
     $form['search'] = [
       '#type' => 'textfield',
       // '#title' => $this->t('Search'),
@@ -66,10 +68,16 @@ class MaterioSapiSearchForm extends FormBase {
     $terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids);
 
     foreach ($terms as $tid => $term) {
-      $childs = $term->get('field_terms')->getValue();
+      if($term->hasTranslation($lang)){
+        $term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $lang);
+      }
       $options = array($term->getName());
+      $childs = $term->get('field_terms')->getValue();
       foreach ($childs as $child) {
         $child_term = \Drupal\taxonomy\Entity\Term::load($child['target_id']);
+        if($child_term->hasTranslation($lang)){
+          $child_term = \Drupal::service('entity.repository')->getTranslationFromContext($child_term, $lang);
+        }
         $options[$child_term->id()] = $child_term->getName();
       }
       $form['filters']['filter-'.$tid] = array(