Exporting and Importing Taxonomy Terms with Drush

  • henok_mikre

    Henok Mikre

The Features module makes it very easy to manage taxonomy vocabularies. Managing taxonomy terms, however, is a bit complicated. That is mainly because terms are considered content. Therefore, one would not wish to manage terms in a feature any more than he or she would manage nodes. If that were the case, then there is always the uuid_features module (see link under "Resources").

Another method would be to export terms to a CSV file. That would allow the developer to easily import terms after each site installation. Here is how we do it:

Our Drupal projects often have a structure similar to Lullabot's Drupal Boilerplate:

{% highlight bash %} ls -A1

data docroot patches scripts

{% endhighlight %}

Our addition here is the "data" directory. That is where we keep CSV, XML or JSON files that need to be imported into the site in order to build a basic instance of the site. Inside the scripts directory, we would have a script called site-install.sh with commands like these:

{% highlight bash %} drush site-install ... drush migrate-import --group=static_pages drush feeds-import news --http=../data/news.xml {% endhighlight %}

And how great would it be to have one more drush command to import taxonomy terms? Here is how:

There is a nice module called Taxonomy CSV import/export. It is a little quirky, but it gets the job done. The current stable version of the module (7.x-5.10) does not appear to work well with Drush 6.2. And that seems to be due to a patch applied to it for compatibility with an earlier version of Drush. The patch calls drush_get_merged_options() and that adds options that the module later complains about. Here is a snippet of the culprit with our debugging lines:

{% highlight bash %} cat taxonomy_csv.drush.inc | awk 'NR >= 173 && NR <= 188' {% endhighlight %}

{% highlight php startinline=true %} // Last check: unknown options. print_r(array_keys(drush_get_merged_options())); $other_options = array_diff(array_keys(drush_get_merged_options()), array_keys($options)); // Compatibility with drush 4.5. See https://drupal.org/node/1395090. if (($i = array_search('drush-make-update-default-url', $other_options)) !== FALSE) { unset($other_options[$i]); } if (($i = array_search('php-options', $other_options)) !== FALSE) { unset($other_options[$i]); } if (($i = array_search('php', $other_options)) !== FALSE) { unset($other_options[$i]); } {% endhighlight %}

The quickest work around is to download the dev version of the module:

{% highlight bash %} drush dl taxonomy_csv-7.x-5.x-dev drush en -y taxonomy_csv {% endhighlight %}

Here is a command to export terms to a csv file.

{% highlight bash %} drush taxocsv-export skills flat ../data/skills.csv

Checking options... Options are good. Launch export process. Please wait... Term 1 of 1 processed: A1 No error, warnings or notices have been reported during export process of 1 terms. 1 / 1 terms of chosen vocabularies have been exported to file taxocsv.csv (0.1 KB) with the format "Term names". Click on link to view it or right click to download it. No duplicate term name has been found. The vocabulary has been exported to ../data/skills.csv. End of drush export batch. {% endhighlight %}

You will notice that the output is a bit verbose. Another issue is that it creates a redundant out-file in the public files directory even though we provided a destination as an argument.

Here is a command to import the terms:

{% highlight bash %} drush taxocsv-import ../data/skills.csv flat --keep_order --vocabulary_id=skills

Checking options... Options are good. Launch import process. Please wait... Line 1 of 1 processed: "A1" No error, warnings or notices have been reported during import process of 1 lines. File "taxocsv.csv" uploaded.Source content: "flat".Existing terms choice: "update".Language of terms: "und". A vocabulary has been used or created.Terms are imported into existing vocabulary "Skills".Hierarchy level has been manually set.Old hierarchy level was 0 (no parent (flat)).Hierarchy level is (no parent (flat)).Properties can be edited at Administer > Structure > Taxonomy > edit vocabulary.You can view terms at Administer > Structure > Taxonomy > list terms. Line checks have been disabled. Some warnings and notices may have not been reported. No more information: choice is to report only first warning or notice if any. End of drush import batch. {% endhighlight %}

Even though the destination vocabulary option is called "--vocabulary_id", you will notice that it also accepts a vocabulary name.

Once the terms are imported, we can verify with a quick db query:

{% highlight bash %} drush sqlq --extra="-t" "SELECT * FROM taxonomy_vocabulary" {% endhighlight %}

{% highlight sql %} +-----+--------+--------------+-------------+-----------+----------+--------+ | vid | name | machine_name | description | hierarchy | module | weight | +-----+--------+--------------+-------------+-----------+----------+--------+ | 1 | Skills | skills | | 0 | taxonomy | 0 | +-----+--------+--------------+-------------+-----------+----------+--------+ {% endhighlight %}

{% highlight bash %} drush sqlq --extra="-t" "SELECT * FROM taxonomy_term_data" {% endhighlight %}

{% highlight sql %} +-----+-----+---------------+-------------+------------+--------+ | tid | vid | name | description | format | weight | +-----+-----+---------------+-------------+------------+--------+ | 1 | 1 | A1 | | plain_text | 0 | +-----+-----+---------------+-------------+------------+--------+ {% endhighlight %}

You can take a look at all of the available options by issuing the following command:

{% highlight bash %} drush help taxocsv-export drush help taxocsv-import {% endhighlight %}

Or, just browse to /admin/help/taxonomy_csv for an extended discussion on the topic.


Let's work together to deliver a success story.