I have two node types: Lesson
and Exercise
. A lesson contains several exercises, which are children. Lessons have a category field which is a reference field of type taxonomy term. Exercises point to lessons through a Lesson reference field, which I suppose indirectly maps exercises to terms.
When I create a lesson, I use the following code:
$node = Node::create([
// The node entity bundle.
'type' => 'lesson',
'langcode' => 'en',
'created' => $lesson->created,
'changed' => $lesson->changed,
'uid' => $lesson->uid,
'title' => $lesson->title,
'field_category' => $tid,
'body' => [
'summary' => $lesson->summary,
'value' => $lesson->body,
'format' => 'full_html',
],
]);
$node->save();
Note the field_category
field.
The above does not result in the taxonomy_index
table being updated. I could update it with an sql query, but I would like to know how to do it via the above core approach.
And for the Exercise
node type, I suppose I need to do likewise, unless my linking of an Exercise
to a Lesson already accomplishes the hierarchy.
How do I alter the above to result in the taxonomy_index
table being updated?
taxonomy_build_node_index
only stores information about a node in thetaxonomy_index
table if the node is published. I don't know if that applies in your case or not.