After loading a node with
Node::load with an existing taxonomy field
abc do I add a new term and save? Something like?
$term = Term::load(123); $node->get('abc')->appendItem($term );
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.Sign up to join this community
This is the correct long form, as in the comment of mradcliffe:
$node->get('abc')->appendItem(['target_id' => 123]);
You don't need to load the term, use the term id.
You can use shorter versions:
$node->abc->appendItem(['target_id' => 123]); $node->abc = ['target_id' => 123]; $node->abc = '123';
Just check the following code, its a dummy code need to be test
use \Drupal\node\Entity\Node; $node = Node::create([ 'type' => 'article', 'title' => 'Druplicon test', ]); $node->set('myterm_field', 'NEW TERM'); $node->save();