Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Drupal: Re-Sync Content Taxonomy from core taxonomy

Parent Feed: 

I'm working on a major Drupal (6) site migration now, and one of the components that needs to be automatically migrated is taxonomies, mapping old vocabularies and terms to new ones. Core taxonomy stores node terms in the term_node table, which is fairly easy to work with. However, two taxonomy supplements are making the process a little more complicated:

First, we're using Content Taxonomy to generate prettier/smarter taxonomy fields on node forms. The module allows partial vocabularies to be displayed, in nicer formats than the standard multi-select field. The problem with Content Taxonomy for the migration, however, is that it duplicates the term_node links into its own CCK tables. If a node is mapped to a term in term_node, but not in the Content Taxonomy table, when the taxonomy list appears on the node form, the link isn't set.

Ideally, the module would have the ability to re-sync from node_term built in. There's an issue thread related to this - Keep core taxonomy & CCK taxonomy synced - but it's not resolved.

So I wrote a Drush command to do this. To run it, rename "MODULE" to your custom module, backup your database, read the code so you understand what it does, and run drush sync-content-taxonomy --verbose.

Warning: This code only works properly on *shared* CCK fields, that is, fields with their own tables (content_field_X tables, not a common content_type_Y table). Don't use this if your fields are only used by one content type.

The other taxonomy supplement that needs to be migrated is Primary Term. I'll be writing a similar Drush script for this in the next few days.

Update 1/27: There was a bug in the way it cleared the tables before rebuilding, should be good now. (Make sure to download the latest Gist.)

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web