How to programatically access the value of a taxonomy term reference? I have a taxonomy term reference field attached to my user account.

I tried getting the value programatically in the same way as I get the value for text fields, but it doesn't work.

use Drupal\Core\Session\AccountProxyInterface;
use Drupal\user\Entity\User;

$account = User::load($account-&gt;id());
// Returns the correct value.
$textfield = $account-&gt;get('field_textfield')-&gt;value;
// Does not return the correct value.
$termreference = $account-&gt;get('field_termreference')-&gt;value;


How am I supposed to get the value (name) of the referenced term programatically?
As the taxonomy term is a reference the value (or label) is not stored in the user entity, just the target_id. So you need to use:

$termreference = $account-&gt;get('field_termreference')-&gt;target_id;

This should be your code:

use Drupal\Core\Session\AccountProxyInterface;
use Drupal\user\Entity\User;

$account = User::load($account-&gt;id());
// Returns the correct value.
$textfield = $account-&gt;get('field_textfield')-&gt;value;
// You need to use the target_id to access to the value.
$termreference = $account-&gt;get('field_termreference')-&gt;target_id;


Now you need to search the label:

$term = \Drupal::entityTypeManager()-&gt;getStorage('taxonomy_term')-&gt;load($termreference);
$name = $term-&gt;getName();


Your taxonomy label (name) will be in $name.