Programmatically accessing a term value

The most oft used properties of a taxonomy term have their own methods, like term::getName(). But what do you do when you need a value that does not?

An example of this is the vocabulary ID (vid). This exists in the term an array of a protected member, values. Because of that, one cannot simply reference it as

$term->values['vid']

but this will work

$vid_array = $term->get('vid')->getValue()

Having done that, the value returned is an array. The value is tucked within it and can be accessed with

$my_term_variable[0]['target_id']

 


Type:
Tip
Tags:
D9
D8
Back-end
taxonomy
Drupal