By Brodie


2012-07-23 15:19:54 8 Comments

A lot of our stuff has uris formatted like site/taxonomy/XX, where XX is an integer.

I found a lot of questions like "how to get the TID from the name?", but I'd like to get the name from the TID. I'm trying to put together a breadcrumb script and everything is great except for these cases where I'm getting a trail like "home > term". Instead I want to have it like "home > <TERM NAME>".

How can I do so?

5 comments

@JHONATAN DAVID FERNANDEZ ROSA 2017-12-11 13:24:48

It is possible to obtain the name of the term in Drupal 8 in the following way:

//Obtain the term.
$tid=1;
$term= taxonomy_term_load($tid);

//get the field name
$term->label();

//or
$term->get('name')->value;

@Anupriya_vij 2017-12-11 11:52:40

For D7:

$term = taxonomy_get_term_by_name($term_name, $vocab_name);  
  foreach($term as $key => $data) {  
    $tid = $data->tid;  

    dpm($tid);  

  }  

For D8:

$term_name = \Drupal\taxonomy\Entity\Term::load(2)->get('name')->value;  
dpm($term_name);  

@oknate 2016-06-02 14:54:07

In Drupal 8, you can get the name of taxonomy terms this way:

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);

$name = $term->label();

Or to load multiple:

$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadMultiple($tids);

foreach($terms as $term) {
  $name = $term->label();
}

@Diana 2017-04-07 13:39:23

I get this error - Notice: Undefined property: Drupal\Core\Entity\Plugin\DataType\EntityReference::$target

@houmem 2015-07-08 07:53:49

The following function returns the name of a taxonomy term based on its tid:

function get_term($tid) {
  return db_select('taxonomy_term_data', 't')
  ->fields('t', array('name'))
  ->condition('tid', $tid)
  ->execute()
  ->fetchField();
}

@Kartagis 2015-07-08 07:57:23

It's not advisable to use database functions when you can achieve the same thing with an API function.

@houmem 2015-10-25 20:17:45

In some case it's better to use database function because the API functions load a big volume of data

@Dimetry 2017-03-18 18:48:47

Also useful when taxonomy access permission modules restrict access

@Clive 2012-07-23 15:23:10

If you're using Drupal 7 you can use taxonomy_term_load()

$term = taxonomy_term_load($tid);
$name = $term->name;

If you've got a bunch of term IDs you can save having to run a single query for each load by using taxonomy_term_load_multiple():

$tids = array(1, 2, 3);
$terms = taxonomy_term_load_multiple($tids);

foreach ($terms as $term) {
  $name = $term->name;
}

If you're stuck using Drupal 6 you can use taxonomy_get_term():

$term = taxonomy_get_term($tid);
$name = $term->name;

There's no multiple-load option for Drupal 6 that I know of unfortunately.

@Brodie 2012-07-23 16:09:20

thank you very very much wish i could extra plus for the version support :D

@Brodie 2012-07-23 16:10:59

additional question: I think this along the same lines, is it pretty easy to set it so the urls read site/<term name> ? or would i have to set that for each individual term?

@Clive 2012-07-23 16:26:23

You can do it on a term-by-term basis but that's pretty laborious. The pathauto module was written just for this sort of situation, you can use it to define patterns to auto-assign aliases to terms/content/users/etc.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Drupal 8 - "Level up" link in taxonomy term view

1 Answered Questions

[SOLVED] How to save term with term references programmatically?

  • 2015-09-28 16:19:59
  • Roomy
  • 517 View
  • 1 Score
  • 1 Answer
  • Tags:   taxonomy-terms

1 Answered Questions

[SOLVED] Where do I put PHP to get a term ID?

  • 2014-04-30 19:49:03
  • Josh G.
  • 46 View
  • 0 Score
  • 1 Answer
  • Tags:   6 taxonomy-terms

1 Answered Questions

Why is my taxonomy term name no longer outputting?

6 Answered Questions

[SOLVED] Taxonomy breadcrumbs

1 Answered Questions

[SOLVED] taxonomy name filter to get terms with special characters

  • 2012-10-19 15:14:27
  • Sven Rishede Philipsen
  • 910 View
  • 1 Score
  • 1 Answer
  • Tags:   7 taxonomy-terms

1 Answered Questions

[SOLVED] Get term from tid in views

1 Answered Questions

[SOLVED] Sort by taxonomy term name in Views?

Sponsored Content