I try to get term name but error:
Error: Call to a member function getName() on null in eval() (line 7 of modules/computed_field/src/Plugin/Field/FieldType/ComputedFieldItemTrait.php(50) : eval()'d code).

use Drupal\taxonomy\Entity\Term;

$tid = $fields['field_book_author'][0]['target_id'];
$term = Term::load($tid);
$book_author = $term->getName();
// $value = $tid --> this return tid 1803
$value = $book_author;

Comments

drupalisme created an issue.

ruslan piskarov’s picture

After loadin need to check if ($term instanceof TermInterface) { // make somethisng }

maxilein’s picture

$term_id= $fields['field_name'][0]['target_id'];
$term_name = \Drupal\taxonomy\Entity\Term::load($term_id)->get('name')->value;
$value = $term_name;