Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add taxonomy terms using Drupal Migrate

I'm using the migrate module to copy data from several sources to a new drupal installation. So far, I'm able to replicate a lot of what I need from the examples provided with the module. I'm currently stuck on adding terms or taxonomy to newly created nodes. The example shows:

// These are related terms, which by default will be looked up by name
$this->addFieldMapping('migrate_example_beer_styles', 'terms')
     ->separator(',');

I've tracked down the migrate_example_beer_styles destination mapping and it seems to be the machine name for that taxonomy.

I've tried imitating this behavior with every variation of what my machine_name should be, but the terms never seem to get associated:

By id:

// where source breed_id is '1,100' - it finds mapped values accordingly
$this->addFieldMapping('breeds', 'breed_id')
     ->sourceMigration('BreedMigration')
     ->separator(',')

And, by name:

// where source breeds is 'Dogs,German Shepherd'
$this->addFieldMapping('breeds', 'breeds')
     ->separator(',');

Am I wrong assuming the destination mapping is the machine name for a taxonomy?

This version of the migrate module was released recently, I haven't found any other helpful examples on the web.

like image 433
labue Avatar asked Dec 20 '10 15:12

labue


People also ask

Where would you add fields for reference taxonomy terms?

To create a new vocabulary and associate it with a content type, just "add new field" of type "term reference." You also can go admin->structure->taxonomy and add a vocabulary there, but (as you discovered) that doesn't glue it to the content type.

What is taxonomy terms in Drupal?

Taxonomy, a powerful core module, allows you to connect, relate and classify your website's content. In Drupal, these terms are gathered within "vocabularies". The Taxonomy module allows you to create, manage and apply those vocabularies. Drupal 7 and 8 has the ability to add taxonomy fields to vocabularies and terms.


2 Answers

This question still seems to be getting some views, so I thought I'd add what else I've discovered. While the accepted answer works, you are able to map Vocabs on ID:

$this->addFieldMapping('Exact Case Sensitive Vocab Name', 'source_field_name')
     ->sourceMigration('ExactSourceClassName')
     ->arguments(array('source_type' => 'tid'))
     ->separator(',');

->separator(',') used for passing a delimited string of source ids. Obviously, leave that off if you're mapping an array of values.

like image 179
labue Avatar answered Sep 21 '22 02:09

labue


I'm currently working with migrate module myself, and I agree that the documentation is somewhat wanting at this point. :)

The 'machine name' of a vocabulary is listed in the Vocabulary table, in the field 'module'. Try using that value. Do note that you need to feed the text into the mapping, not the ids.

like image 33
John Fiala Avatar answered Sep 21 '22 02:09

John Fiala