Create term with specific tid

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
lucio.ferrari's picture

Hi all!

I'd like to know whether there is a way (via code) to force a specific tid when creating a term, or a specific vid when creating a vocabulary.

I'm managing multiple (cloned) Drupal sites, and I can create a vocabulary and a term using Drush and a custom PHP script:

<?php
  $vocab
= array(
   
'name' => t('my new vocab'),
   
'description' => t('whatever'),
   
'nodes' => array('my_type_X' => 1,
                    
'my_type_Y' => 1,
                    
'my_type_Z' => 1),
   
'hierarchy' => 0,
   
'relations' => 0,
   
'tags' => 0,
   
'multiple' => 0,
   
'required' => 0,
   
'weight' => 5,
  );

 
taxonomy_save_vocabulary($vocab);

 
$term = array(
   
'vid' => 5, // hopefully it's the vocabulary created above
   
'name' => 'my_term',
  );

 
taxonomy_save_term($term);

?>

Then I'll deploy with drush as:
drush -r /var/www/my_folder -l http://www.mysite.com scr custom_script.php
(this 27 times, once for each site, it sure beats manually creating a vocabulary and a term on each site's admin interface).

Now, these 27 sites are not exactly the same (not anymore), so there could be a difference in vocabulary terms' tid. According to the database settings every tid field is 'auto-increment', but does anybody know a way to create a term that has a specific tid? Something like

$term = array(
    'vid' => 5, // hopefully it's the vocabulary created above
    'name' => 'my_term',
    'tid' => 20,

Of course, a solution could be to create the term and then mess with the table "term_data", but I'd prefer to have a "all-php" solution, better using an existing API function.

Thanks in advance

L.F.