Skip to content
Snippets Groups Projects
Commit 78e257a1 authored by Vladimir Roudakov's avatar Vladimir Roudakov
Browse files

Issue #3346925 by vladimiraus, mkalkbrenner: Term translation errors and performance issues

parent 04bff11f
Branches 2.0.x
Tags 2.0.2
1 merge request!28Term translation errors
Pipeline #386370 failed
...@@ -321,10 +321,12 @@ function _term_reference_tree_get_children($tid, $vid) { ...@@ -321,10 +321,12 @@ function _term_reference_tree_get_children($tid, $vid) {
$table = 'taxonomy_term_field_data'; $table = 'taxonomy_term_field_data';
$alias = 't'; $alias = 't';
$default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
$query = \Drupal::database() $query = \Drupal::database()
->select($table, $alias); ->select($table, $alias);
$query->join('taxonomy_term__parent', 'p', 't.tid = p.entity_id'); $query->join('taxonomy_term__parent', 'p', 't.tid = p.entity_id');
$query->fields('t', ['tid', 'name', 'status']); $query->fields('t', ['tid', 'name', 'langcode', 'status']);
$query->addField('t', 'vid', 'vocabulary_machine_name'); $query->addField('t', 'vid', 'vocabulary_machine_name');
$query $query
->condition('t.vid', $vid) ->condition('t.vid', $vid)
...@@ -334,11 +336,21 @@ function _term_reference_tree_get_children($tid, $vid) { ...@@ -334,11 +336,21 @@ function _term_reference_tree_get_children($tid, $vid) {
->addTag('term_reference_tree_get_children') ->addTag('term_reference_tree_get_children')
->orderBy('t.weight') ->orderBy('t.weight')
->orderBy('t.name'); ->orderBy('t.name');
if (\Drupal::moduleHandler()->moduleExists('locale')) {
$query->condition('t.langcode', [
\Drupal::languageManager()->getCurrentLanguage()->getId(),
$default_langcode,
], 'IN');
}
$result = $query->execute(); $result = $query->execute();
$terms = []; $terms = [];
while ($term = $result->fetchObject()) { while ($term = $result->fetchObject()) {
$terms[$term->tid] = $term; if (!isset($terms[$term->tid]) || $terms[$term->tid]->langcode === $default_langcode) {
$terms[$term->tid] = $term;
}
} }
return $terms; return $terms;
...@@ -442,25 +454,11 @@ function _term_reference_tree_build_level($element, $term, $form_state, $value, ...@@ -442,25 +454,11 @@ function _term_reference_tree_build_level($element, $term, $form_state, $value,
function _term_reference_tree_build_item($element, $term, $form_state, $value, $max_choices, $parent_tids, $parent, $depth) { function _term_reference_tree_build_item($element, $term, $form_state, $value, $max_choices, $parent_tids, $parent, $depth) {
$leaves_only = FALSE; $leaves_only = FALSE;
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$t = NULL;
if (\Drupal::moduleHandler()->moduleExists('locale') && !empty($term->tid)) {
$t = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->load($term->tid);
if ($t && $t->hasTranslation($langcode)) {
$term_name = $t->getTranslation($langcode)->label();
}
}
if (empty($term_name)) {
$term_name = $term->name;
}
$container = [ $container = [
'#type' => 'checkbox_tree_item', '#type' => 'checkbox_tree_item',
'#max_choices' => $max_choices, '#max_choices' => $max_choices,
'#leaves_only' => $leaves_only, '#leaves_only' => $leaves_only,
'#term_name' => $term_name, '#term_name' => $term->name,
'#level_start_minimized' => FALSE, '#level_start_minimized' => FALSE,
'#select_parents' => $element['#select_parents'], '#select_parents' => $element['#select_parents'],
'#depth' => $depth, '#depth' => $depth,
...@@ -469,7 +467,7 @@ function _term_reference_tree_build_item($element, $term, $form_state, $value, $ ...@@ -469,7 +467,7 @@ function _term_reference_tree_build_item($element, $term, $form_state, $value, $
if (!$element['#leaves_only'] || count($term->children) == 0) { if (!$element['#leaves_only'] || count($term->children) == 0) {
$e = [ $e = [
'#type' => ($max_choices == 1) ? 'radio' : 'checkbox', '#type' => ($max_choices == 1) ? 'radio' : 'checkbox',
'#title' => $term_name, '#title' => $term->name,
'#on_value' => $term->tid, '#on_value' => $term->tid,
'#off_value' => 0, '#off_value' => 0,
'#return_value' => $term->tid, '#return_value' => $term->tid,
...@@ -490,7 +488,7 @@ function _term_reference_tree_build_item($element, $term, $form_state, $value, $ ...@@ -490,7 +488,7 @@ function _term_reference_tree_build_item($element, $term, $form_state, $value, $
else { else {
$e = [ $e = [
'#type' => 'checkbox_tree_label', '#type' => 'checkbox_tree_label',
'#value' => $term_name, '#value' => $term->name,
]; ];
} }
...@@ -582,6 +580,8 @@ function _term_reference_tree_get_parent($tid) { ...@@ -582,6 +580,8 @@ function _term_reference_tree_get_parent($tid) {
/** /**
* Helper function to output a single level of the term reference tree display. * Helper function to output a single level of the term reference tree display.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/ */
function _term_reference_tree_output_list_level(&$element, &$tree) { function _term_reference_tree_output_list_level(&$element, &$tree) {
$tree_element = []; $tree_element = [];
...@@ -600,26 +600,19 @@ function _term_reference_tree_output_list_level(&$element, &$tree) { ...@@ -600,26 +600,19 @@ function _term_reference_tree_output_list_level(&$element, &$tree) {
if (!$term instanceof TermInterface) { if (!$term instanceof TermInterface) {
continue; continue;
} }
$url = $term->toUrl();
$uri['options']['html'] = TRUE;
$class = $item['selected'] ? 'selected' : 'unselected';
$t = NULL; if (\Drupal::moduleHandler()->moduleExists('locale') && !empty($term->tid)) {
$term_name = ''; if ($term->hasTranslation($langcode)) {
if (\Drupal::moduleHandler() $term = $term->getTranslation($langcode);
->moduleExists('locale') && !empty($term->tid)) {
$t = $term;
if ($t && $t->hasTranslation($langcode)) {
$term_name = $t->getTranslation($langcode)->label();
} }
} }
if (empty($term_name)) {
$term_name = $term->label(); $class = $item['selected'] ? 'selected' : 'unselected';
}
$tree_element['#items'][] = [ $tree_element['#items'][] = [
'#type' => 'link', '#type' => 'link',
'#title' => $term_name, '#title' => $term->label(),
'#url' => $url, '#url' => $term->toUrl(),
'#attributes' => ['class' => [$class]], '#attributes' => ['class' => [$class]],
]; ];
if (isset($item['children'])) { if (isset($item['children'])) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment