2

I've a csv with taxonomy terms, with tids. I've to import, with a module, these terms in a new drupal installation, but if I try to set the tid programmatically, the module does not create the term. Instead, if I don't set the tid, the term is created with but with the autoincremental tid. This code doesn't work:

$term = new stdClass();
$term->name = 'Test';
$term->vid = 3;
$term->tid = 10; // if I remove this line the term is created
taxonomy_term_save($term);

The doc says: tid: (optional) The unique ID for the term being saved. If $term->tid is empty or omitted, a new term will be inserted.

Is there a way to set the tid programmatically? Or is there a module that do this?

2

If you set $term->tid for a taxonomy term you save with taxonomy_term_save(), the function will take you are updating an existing taxonomy term, with the consequence that the taxonomy term is not saved.

You could change the taxonomy term ID for the taxonomy term you right saved, but there is the risk of conflict with an existing taxonomy term you didn't import. You should change more that one database table, and you should let other modules handling data related to taxonomy terms know they need to update their data, which means you probably should make those modules think a taxonomy term is deleted, and a new taxonomy term is created. See also the code for taxonomy_term_delete() to understand what Drupal does when a taxonomy term is deleted.

If you really need to export taxonomy terms from a site, and import them in a different Drupal site, you should be interested to the Universally Unique Identifier module. In that way, the taxonomy term exported from site A, and imported in site B will have the same UUID.
In Drupal 8, any entity defined from Drupal has a UUID. The schema definition used for taxonomy terms in Drupal 8, for example, is the following one.

  $schema['taxonomy_term_data'] = array(
    'description' => 'Stores term information.', 
    'fields' => array(
      'tid' => array(
        'type' => 'serial', 
        'unsigned' => TRUE, 
        'not null' => TRUE, 
        'description' => 'Primary Key: Unique term ID.',
      ), 
      'uuid' => array(
        'description' => 'Unique Key: Universally unique identifier for this entity.', 
        'type' => 'varchar', 
        'length' => 128, 
        'not null' => FALSE,
      ), 
      'vid' => array(
        'type' => 'int', 
        'unsigned' => TRUE, 
        'not null' => TRUE, 
        'default' => 0, 
        'description' => 'The {taxonomy_vocabulary}.vid of the vocabulary to which the term is assigned.',
      ), 
      // …
    ),
    // …
  );
  • I've a site and I've to copy nodes to another installation. I'm using node_export but node_export uses tid to keep terms, so in the second installation I've to find terms with tid from "old" installation. I can also use feeds to import content, and in this way I solve, but node_export is faster.. – arrubiu Aug 20 '12 at 19:06
0

A possible answer to this is that the terms added with a set is is conflicting with a previous tid. Tids numbering are not vocabulary specific. If another vocabulary is already using a tid you are trying to add, there will be conflict.

Th code you are using is in itself correct, but make sure you don't get any possible duplicates.

0

There is a related issue on Drupal.org.

You can parch the Drupal core with the mentioned fix.

Afterwards you can use the nex code:

  $name = 'TERM NAME';
  $tid = 123;
  $vocabulary_name = 'tags';

  if (!taxonomy_term_load($tid) && taxonomy_get_term_by_name($name)) {
    $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name);

    $new_term = entity_create('taxonomy_term', array(
      'name' => $name,
      'tid' => $tid,
      'is_new' => TRUE,
      'vid' => $vocabulary->vid,
      'description' => ''
    ));
    taxonomy_term_save($new_term);
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.