1

I have a custom argument validator for Views, which expects a term name input and outputs the tid for the query. This works great in English, but I'm not sure what to do for translations. Is there any way to search for a term id from the translated term name?

Here's my code:

public function validateArgument($argument) {

  if(strpos($argument, '-')){
      $argument = str_replace('-', ' ', $argument);
  }
  $term = taxonomy_term_load_multiple_by_name($argument);
  $termId = key($term);
  $this->argument->argument = $termId;

  return TRUE;

} 
1

A suggestion :

$my_translated_term_name = 'astuce';

$query = \Drupal::database()->select('taxonomy_term_field_data', 'td');
$query->addField('td', 'tid');
$query->condition('td.name', $my_translated_term_name);
// For better performance, define the vocabulary where to search.
// $query->condition('td.vid', $vid);
$term = $query->execute();

$tid = $term->fetchField();
  • This seems to work (thank you!). Could I use this to fetch both translated and untranslated names? Would that be efficient/make sense? – andileco Nov 21 '18 at 15:32
  • Meant to say tids from both translated and untranslated names* – andileco Nov 21 '18 at 15:42
  • yes you can get tids for translated and untranslated terms – izus Nov 21 '18 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.