1

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?

1
  • In my case, I found that taxonomy_build_node_index only stores information about a node in the taxonomy_index table if the node is published. I don't know if that applies in your case or not.
    – thirdender
    Commented Oct 1, 2016 at 3:32

1 Answer 1

1

I ended up figuring this out. here is what I did:

$result=db_query("SELECT nid FROM node WHERE type = 'lesson'");
foreach($result as $row){
    $node=entity_load('node', $row->nid);
    $result =  taxonomy_build_node_index($node);
}

This ensures that my lesson node is tracked in the taxonomy_index table. I should probably start learning the class-based approach, but for now this works.

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.