I'm writing a custom Drupal Console command to import user data from a text file and create users, and as part of the process, I need to add data to a term reference field. In the case that a term in the data doesn't exist, I need to create the term and then add the new tid to the the field. I'm using the following code to create the term:
$term = Term::create([
'name' => $member[$roster_key],
'vid' => 'topic_area',
])->save();
The problem is that for some reason, the save()save()
method only returns a boolean indicating if the save was successful, NOT the created term object. This means that now that my term has been created, I have to go do an entity query just to get the object I just created.
Is there a simple way to get the just-created term in my code above, or do I really have to do a separate query?
I would really love to somehow take advantage of the auto_create code behind the "Create referenced entities if they don't already exist" checkbox on the form for adding a new term, but I haven't figured that one out yet.
Thanks.