Exporting and Importing Taxonomy Terms with Drush

Mar 18, 2015. | By: 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:

ls -A1

data
docroot
patches
scripts

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:

drush site-install ...
drush migrate-import --group=static_pages
drush feeds-import news --http=../data/news.xml

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:

cat taxonomy_csv.drush.inc | awk 'NR >= 173 && NR <= 188'
  // 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]);
  }

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

drush dl taxonomy_csv-7.x-5.x-dev
drush en -y taxonomy_csv

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

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.

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:

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.

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:

drush sqlq --extra="-t" "SELECT * FROM taxonomy_vocabulary"
+-----+--------+--------------+-------------+-----------+----------+--------+
| vid | name   | machine_name | description | hierarchy | module   | weight |
+-----+--------+--------------+-------------+-----------+----------+--------+
|   1 | Skills | skills       |             |         0 | taxonomy |      0 |
+-----+--------+--------------+-------------+-----------+----------+--------+
drush sqlq --extra="-t" "SELECT * FROM taxonomy_term_data"
+-----+-----+---------------+-------------+------------+--------+
| tid | vid | name          | description | format     | weight |
+-----+-----+---------------+-------------+------------+--------+
|   1 |   1 | A1            |             | plain_text |      0 |
+-----+-----+---------------+-------------+------------+--------+

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

drush help taxocsv-export
drush help taxocsv-import

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

Resources:

Subscribe

Subscribe to this blog via RSS.

Recent Posts

About

BLEN Corp is a small, minority, and veteran-owned information technology firm located in Washington D.C. Since 2004, we have been ahead of the curve in early adaption and implementation of cutting edge technologies including web and mobile development, service-oriented architecture, and other innovative web based solutions. Look at some of our projects.

Social Links

Our Bunker

WeWork Wonder Bread Factory
641 S St. NW
3rd Floor
Washington, DC 20001
United States.