I have done:
$term = Term::create([ 'name' => 'title', 'vid' => 'myvocab', ])->save();
But I don't seem to be able to set the parent term.
I have tried:
$term->parent = array($parent_tid); $term->save();
It gives an error:
InvalidArgumentException: Value is not a valid entity. in Drupal\Core\Entity\Plugin\DataType\EntityReference->setValue() (line 106 of /core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php).
I have also tried:
$term->parents = array($parent_tid); $term->save();
It gives no error, but also does nothing.
In my code $term could be a new term or an existing term I've loaded, which is why I set the parent and then save it. But I can't get it to work.
Documentation for D8 is sparse. I've dug around the code in the taxonomy module, and it looks like
parent is an entity reference field that accepts multiple values, but I can't work out how to set it. It looks like setting
parent to array($id) should work, but I think the magic setter is breaking. Any help?