I have a webapp I'm building, and I'd like to use taxonomy to organize different nodes used by the webapp.
I've implemented a few content types in a few modules, with a "project" content type being a 'parent' to a series of other content types, all used in the webapp for various processes.
Currently, when a project is created, it's title becomes a taxonomy term with no parent. I do this programmatically when the project node is created.
Additionally, the project's create/edit form has the default taxonomy widget, set for users to create new tags via entering comma separated lists.
This creates the tags fine, but without my project-title-tag as the parent.
I thought that I could walk the tags myself during my validation function, and create them as child to my project-title-tag, but that gives me the user-contributed tags twice: once without a parent and once with my desired parent.
Is there anyway, some syntax I can rewrite the comma-separated-tags string with to give me my tags as child to the correct parent term?
Comments
yes it is
The solution is surprisingly simple, sorta:
The default Drupal 'free tagging' widget puts the user's taxonomy tags into here in the form_state array:
$form_state['values']['taxonomy']['tags'][vocabulary id] = just a string, of, comma, separated, tags
However, that structure does not allow for parenting.
So, I take that string of tags and explode() it into an array and then look for any existing tags with that name via taxonomy_get_term_by_name(). Taxonomy_get_terms_by_name() returns an array of possible matches, so one needs to walk that list looking for a match that has my desired hierarchy parent. If I find a match, a make note of it's tid.
If I don't find a tag with a name from the original comma-separated-tags-string, then I need to create that tag with my desired hierarchy parent, and make note of the created terms tid.
(This is a great resource for tag manipulations:
http://qandeelaslam.com/blogs/drupal/programmatically-manipulating-taxon... )
Finally, after all that, I have an array of term ids to existing terms, all of which are child to my parent term.
Now I unset( $form_state['values']['taxonomy']['tags'][vocabulary id] )
and set $form_state['values']['taxonomy'] = my-array-of-term-ids
This moves and reformats the users input from the free tagging back-end to a more complicated hierarchical back-end that is simply too hard to explain to people, but they sure love it when it 'just works'. This effectively provides your users with the nice and simple 'free tagging' taxonomy widget, while behind the scenes maintains a hierarchy for the tags. Meaning the same tags from different parts of the hierarchy don't get confused (confusing your user's organization of their data), and a bunch of other good stuff for organizing and working with the data.
-Blake
www.BlakeSenftner.com www.3D-Avatar-Store.com