Bundle Copy for Drupal Taxonomy Terms

We had a simple problem: capture a Drupal taxonomy AND its associated terms (definition AND content) in code so that we could automatically deploy a new taxonomy based feature to a website.

Bundle Copy was on our radar as the solution. Or at least part of it.

Like the Views export-import model, Bundle Copy provided an export screen that displayed generated code that we could copy-paste into a deployment module. It provided the first half of our solution: generated code. All we needed to add was a simple consumption framework that could digest and process the generated code. But Bundle Copy only provided Taxonomy definition code. Bundle Copy did not generate Taxonomy content (terms). Bundle Copy only solved half of the code generation problem. We were missing code that could recreate the Taxonomy terms. Content is King!

Sound like a simple problem. All we needed to do was extend Bundle Copy to also generate code for the taxonomy content (terms). Simple in concept. And at first glance it seemed much simpler than trying to go back to Features and extending the much more complex Features UI to include taxonomy terms. Sticking with Bundle Copy seemed safer?

The Bundle Copy UI provided a very simple export form that had a large text box that contained the Taxonomy definition in raw PHP code. Only consumable by Bundle Copy.  How to extend this? My hook_form_alter brain kicked in and identified that the simplest solution required the addition of a single additional text field containing raw PHP code that defined the terms. Consumption of the PHP code was a separate problem.

The solution appeared obvious:

  • create a module that extended Bundle Copy via hook_form_alter
  • add another text field to the form
  • generate PHP code that defines the term content
  • display the PHP code in the new form text field

This model then depended upon creation of a deployment framework that also extended the Bundle Copy module. Basic concept was:

  • execute the new & improved Bundle Copy extension
  • copy paste the taxonomy definition code into placeholder module
  • copy paste the taxonomy content code into placeholder module
  • provide a generic framework to execute the taxonomy definition and content code

All we had to do was create code to generate the Taxonomy content code and then consume it.

This ask seemed daunting until we looked at the internals of Bundle Copy. Seems like ctools_var_export was very popular. It was a wrapper that generated very nice PHP code from definition data structures. This resulted in the creation of a mechanism that exploited taxonomy_vocabulary_machine_name_load followed by taxonomy_get_tree followed by ctools_var_export. This gave us nicely formed raw PHP code that defined all of the Taxonomy terms,

Time to consume the generated code. This ended up being a very simple loop that processed the generated code and created each term via a taxonomy_term_save call.

Anti-climatic. A big problem that resulted in a very few simple calls to some low level taxonomy primitives. The devil is in the details... Bundle Copy is your friend...

P.S. If you have a similar problem, please feel free to reach out to me. I may be able to provide additional details to help solve your problem.