Problem/Motivation
It is possible to have two terms with the same names at a same level of the hierarchy. (i.e. two terms with the same name and the same parent term). Duplications can lead to problem in modules that rely on taxonomy to create say menus or url paths.
Proposed resolution
Modify taxonomy_save_term so that it checks if inserting/updating the term will create a duplication and abort the operation if needed.
Remaining tasks
Because of the age of this issue, I recommend the following tasks:
| Task | Novice task? | Contributor instructions | Complete? |
|---|---|---|---|
| Update the issue summary | Instructions | Yes | |
| Add steps to reproduce the issue | Novice | Instructions | |
| Embed before and after screenshots in the issue summary | Novice | Instructions | |
| Manually test the patch | Novice | Instructions | |
| Reroll the patch if it no longer applies. | Instructions |
User interface changes
The patch for taxonomy.admin.inc notifies the admin of the duplication problem when it happens.
API changes
The patch for taxonomy.module modifies taxonomy_save_term so that it checks if inserting/updating the term will create a duplication and abort the operation if needed.
| Comment | File | Size | Author |
|---|
Comments
Comment #1
twiinz commentedComment #2
macgirvin commented+1
This has been a thorn in my side for years - see #41043: taxonomy.module - duplicate terms allowed in same vocabulary; though I found aggregator2 to be quite unstable at the time and don't use it anymore.
If you follow the comments on that issue, you'll find that it 'works as designed' even though I vehemently disagree. I've got no problem with duplicate terms across vocabularies, or within other branches of the tree, but to be on the same branch is pointless and confusing. A proposed solution was to run a perl script to merge these back together again - but they shouldn't happen in the first place and I don't know many site admins that want to start their day looking for duplicate terms which got inserted via an RSS feed and manually running scripts to set things straight.
I note that the way aggregator2 once worked, this patch would give it grief as it blindly created a term and used the returned ID from the create call to categorize external feed postings. Failures weren't checked or acted on properly. Hopefully nobody does this anymore and they actually check for existence of terms before using them or creating them. But I note that since you've found this bug, you've found something that didn't play along - hopefully it was a manual entry and not another module to be fixed. But there may be good reason in the taxonomy API (if that still exists) to just return the existing term ID rather than a duplicate error.
Comment #3
twiinz commentedFixed a typo present in the previous patch (taxonomy.admin.inc).
Sorry about that.
Comment #4
twiinz commentedI slept on it and realized that the patch could cause the following problem:
Context:
- Only one term has a parent
- One tries to move this term to the root (which should give a flat vocabulary hierarchy) but the attempt fails because it would duplicate with a term already present at the root.
Expected result:
- The term shouldn't be moved and the admin should be notified (works with previous patch)
- The vocabulary hierarchy should still be set to single, i.e. not flat (doesn't work with previous patch)
Problem:
- Whether the vocabulary hierarchy is flat or single is calculated before any attempt to save the term to the database
- Therefore the following block of code doesn't work as expected because $hierarchy is set to 0 whereas it should still be set to 1
Solution:
- When a term can't be moved because it would create a duplicate, we check if it has a parent and update $hierarchy appropriately.
Cleaned the patch a little bit. There should be no more side effects. I'm not native English speaker so the 3 additional calls to drupal_set_message and watchdog might need some love.
Comment #5
twiinz commentedLearning more and more about drupal and found out that there was a better way to implement this: form validation functions.
This new patch doesn't require modifying taxonomy_save_term, so there is no more need to patch taxonomy.module.
It doesn't modify the two submit functions neither, it just adds the necessary code to taxonomy_form_term_validate and creates the new function taxonomy_overview_terms_validate.
From a usability point of view it's also a lot better as the user is now notified of the problem as he's editing the forms and can therefore take appropriate action.
This patch also support terms with multiple parents which wasn't the case with the previous one.
This should be a much cleaner cut.
Comment #6
webchick+1. I'll try and review this when I get some time.
Still applies with offset.
Comment #7
catchThis needs to go into Drupal 7 first, and also needs updating for the new database layer. Good change though.
Comment #8
marcingy commentedBumping to d8 this is still an issue.
Comment #9
marcingy commentedAdding tag
Comment #10
jibranLast patch is 5 years old I think it needs reroll and some tests for new functionality. I don't think it is a bug It should be a task.
Comment #11
amytswan commentedI'm at DrupalConLA's code sprint - checking out this issue to reroll the last patch
Comment #12
amytswan commentedWhen I rerolled I discovered, yes, there are issues. Patch is now 7 years old. Here's the output from my Terminal:
Removing "needs reroll" tag.
Comment #13
jcandan commentedAlso at DrupalConLA's code sprint. Noticed that the above was kicked by git for trailing whitespace. Will update patch and remove whitespace.
Comment #14
jcandan commentedNever mind the whitespace, at the bottom of the terminal output is a
error: taxonomy.admin.inc: does not exist in index. It seems that the patch wasn't attempted in a directory where it recognized the path. Attempting reroll.Comment #15
jcandan commentedCan't find file for patching. It's pretty old. I'm pretty sure that, although it might still be an issue in D8 as pointed out by #8, it's not applicable to D8. Recommending we mark as Closed (cannot reproduce).
Comment #23
jcandan commentedActually, marking back to D7, and removing "needs backport to D7". This should have never been bumped to D8. If the issue did exist in D8, then a new ticket should have been created detailing the quickest path to recreate the problem.
Comment #24
jcandan commentedUpdate description via dreditor templates. Update Remaining tasks list.