$term = Term::load($_tid);
$term->field_league_id = 'ID value';

I am updating the terms by the code above but it's not working. Create the term by term->Save() is works normally. Everyone had experienced with that please help.


$term = Term::load($_tid);

This solution is not working, it's throwing:

Call to a member function setValue() on a non-object in..

At the top, I declared ref. already.

use Drupal\taxonomy\Entity\Term;

The code for create new taxonomy is working fine, but not for updating.

$term = Term::create([
            'name' => $league->caption,
            'vid' => 'league',
            'field_league_id' => $league_id,
            'field_league_code' => $league->league,
            'field_league_year‎' => $league->year,
            'field_league_numberofteams' => $league->numberOfTeams,


2 Answers 2

$term = Term::load($_tid);
$term->field_league_id->setValue('ID value');

Additional info (requested in comments):

If you print out the object, you'll see that fields use the class core/lib/Drupal/Core/Field/FieldItemList.php.

By browsing through the methods, you'll see a few functions for interacting with the FieldItemList object, such as setValue(),

When I'm developing in Drupal 8, in order to learn the API, I use the

dump() function a lot to see what type of object I'm dealing with, and to inspect it's methods.

dump($term); die();

In fact, in my settings.local.php file I have an include

include 'my_settings.php';

and in there I have this function:

function nprint($obj) {

This is something I've been using for years to inspect objects. Now that drupal is much more object-oriented, printing out objects to find what class they use is essential.

Another essential tool for me is using an editor that allows searching by file name (I use the free software Aptana Studio 3).

So if you see you're dealing with a "FieldItemList", you can do "command + t" to search by file name and type in "FieldItemList". This saves a lot of web-based research. And it helps you learn the code base.

enter image description here

  • can you add some explanation? Sep 28, 2016 at 15:37
  • Where is the answer within this?
    – Kevin
    Sep 28, 2016 at 18:44
  • I would advise using PHPStorm to walk through methods and class inspection... its far more advanced at it than Aptana. I would also advise using xdebug over dump() and die() statements.
    – Kevin
    Sep 28, 2016 at 18:46
  • Yes, that's faster. I should do that. I like Aptana because it's free.
    – oknate
    Sep 28, 2016 at 18:48
  • 1
    What type of field is field_league_id, text? or entity reference? If it's entity_reference you may need to do setValue(['target_id' => $league_id]);
    – oknate
    Sep 29, 2016 at 15:34

Here is a simple example:

$term = \Drupal\taxonomy\Entity\Term::load($tid);
if (!empty($term)) {
  $term->field_league_id = 123; // For multivalue fields, use array().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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