Problem/Motivation

I want to create terms with a pre-defined TID (not using the default increment of the DB). So I call the taxonomy_term_save($term) with a (non-existing) value for $term->tid. The save function checks for an original file to update but can't find one (which is OK), but when deciding on the INSERT or UPDATE operation, a check for the $term->tid is made. This leads to a situation where an UPDATE is done instead of an INSERT.

Proposed resolution

To me it would make more sense to either

  • check for the $term->original value instead of $term->tid
  • make it more obvious that you're not supposed to set the TID yourself

To me the patch is very simple (line 604 in taxonomy.module):

function taxonomy_term_save($term) {
  ...
  // Load the stored entity, if any.
  if (!empty($term->tid) && !isset($term->original)) {
    $term->original = entity_load_unchanged('taxonomy_term', $term->tid);
  }
  ...
  module_invoke_all('entity_presave', $term, 'taxonomy_term');

-  if (empty($term->tid)) {
+  if (empty($term->tid) || !$term->original) {
    $op = 'insert';
    ...
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

xandeadx’s picture

I had the same problem

Berdir’s picture

THis is not supported IMHO. tid is an autoincrement/serial field that you are not supposed to set to predefined values. What if you happen to end up using a tid that is already in use by the system? Then you would accidently override an existing row.

The proper fix for this would imho be #549898: Machine readable names for taxonomy terms, I suggest closing this one as a won't fix or duplicate of that one.

mrfelton’s picture

Version: 7.9 » 7.x-dev

What about when migrating content, say from an old drupal site into an existing one using migrate module. In that case, it's important to be able to retain the tid. Currently this isn't possible. Related issue at #1802286: Impossible to migrate taxonomy terms with specified tid

a.milkovsky’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
575 bytes

Agree with mrfelton. I used the node_save() approach.

a.milkovsky’s picture

Added !empty($term->is_new)