7

According to another post, I should do this $term = \Drupal::entityTypeManager()->getStorage('taxonomy')->load($tid); But then I get this error :

Notice: Undefined property: Drupal\Core\Entity\Plugin\DataType\EntityReference::$target
23

Or you can simply use The entity Term class:

use Drupal\taxonomy\Entity\Term;

and in your code:

$term = Term::load($tid);
$name = $term->getName();
  • this one worked for me . – Diana Apr 7 '17 at 14:44
  • 2
    My first instinct upon looking at this was "oh no, what about the dependency injection they keep hitting us over the head with in Drupal-land thesedays?", but - on inspection - the static ::load($id) method shared by all types of entities has all of the necessary dependency injection support built in - so this would now be my pick of how to load an entity of any kind (node, comment etc). In fact it beats me why anyone would use the long-hand \Drupal::entityTypeManager()->blah->blah->blah method now! (maybe someone else will chime in and give a good reason for that?) – Daniel Scott Oct 4 '18 at 0:23
11

It should be:

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

taxonomy_term vs taxonomy.

Then, $term->getName().

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