Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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';
...
}
Comment | File | Size | Author |
---|---|---|---|
#5 | core-allow_to_set_taxonomy_tid_on_insert-1329554-5.patch.txt | 583 bytes | a.milkovsky |
Comments
Comment #1
xandeadx CreditAttribution: xandeadx commentedI had the same problem
Comment #2
BerdirTHis 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.
Comment #3
mrfelton CreditAttribution: mrfelton commentedWhat 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
Comment #4
a.milkovskyAgree with mrfelton. I used the node_save() approach.
Comment #5
a.milkovskyAdded !empty($term->is_new)