I have a custom module to that generates some sample content for my site upon installation of the module.
I am having difficulty coming up with the correct code to create a new taxonomy term, a tag, and then add it to a node with a term reference field.
Any help would be appreciated.
Comments
Hey Tim 👋 🙂,
Have you tried these?:
Well, I found a Backdrop CMS test that created a vocabulary and some terms and that was really helpful.
I was then able to do this to add it to a node programmatically (see here for the rest of the code to add a node).
I haven't figured out how to add a term to a node without knowing the TID.
I would think that I could add a term to the vocabulary, just by adding it to the node directory. But, I could not get that to work. Ideas?
(note: untested) ...I believe that after
taxonomy_term_save()
, you should be able to do something like$term_id = $term->id;
(or could be$term_id = $term->tid;
- you need to check) to get the term ID; so then it would simply be a matter of:...or directly, w/o assigning it to a variable:
Please give that a go and let us know if it worked for you.
A Google search helped me find this thread, and just wanted to confirm that the above code for creating a new term works in Backdrop 1.16.2, and you can assign the newly created term to a node field like this: