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
- The Taxonomy module uses the numbers
0
,1
, and2
to denote flat, single, and multiple taxonomy vocabulary hierarchy, respectively. See taxonomy_check_vocabulary_hierarchy() and taxonomy_schema(). - It is not clear from the code what these numbers mean or why they are used.
Proposed resolution
- Define constants for the three types of hierarchy.
- TAXONOMY_HIERARCHY_DISABLED = 0
- TAXONOMY_HIERARCHY_SINGLE = 1
- TAXONOMY_HIERARCHY_MULTIPLE =2
- Convert the integers to these values where appropriate. I grepped and found the following instances for 2, which should help:
taxonomy.admin.inc:371: if ($vocabulary->hierarchy < 2 && count($tree) > 1) { taxonomy.admin.inc:405: if ($vocabulary->hierarchy < 2 && count($tree) > 1) { taxonomy.install:151: 'description' => 'The type of hierarchy allowed within the vocabulary. (0 = disabled, 1 = single, 2 = multiple)', taxonomy.module:530: * will be given a hierarchy of 2. taxonomy.module:551: $hierarchy = 2; taxonomy.admin.inc:705: '#collapsed' => $vocabulary->hierarchy < 2, taxonomy.admin.inc:843: elseif ($current_parent_count > $previous_parent_count && $form['#vocabulary']->hierarchy < 2) { taxonomy.admin.inc:844: $form['#vocabulary']->hierarchy = $current_parent_count == 1 ? 1 : 2; taxonomy.module:530: * will be given a hierarchy of 2. taxonomy.module:551: $hierarchy = 2;
Also possibly in
taxonomy.test
, so check for a hierarchy test there as well.
Remaining tasks
- TBD
User interface changes
- None.
API changes
- Constants added:
- TAXONOMY_HIERARCHY_DISABLED
- TAXONOMY_HIERARCHY_SINGLE
- TAXONOMY_HIERARCHY_MULTIPLE
Comments
Comment #1
xjmTagging.
Comment #2
chris.leversuch CreditAttribution: chris.leversuch commentedI'll have a go at this one.
Comment #3
chris.leversuch CreditAttribution: chris.leversuch commentedFirst attempt
Comment #4
chris.leversuch CreditAttribution: chris.leversuch commentedForgot to include grep results for review. I don't think the remaining occurances need changing
Comment #5
acouch CreditAttribution: acouch commentedAwesome start @chris.leversuch. I noticed a couple of small things. There was an extra space before the the multiple constant, I added the constants for the cases in the help section, and have a question about the following:
Shouldn't that be left out since it is referring to the value of the parent?
Comment #6
xjmI think @acouch is right about #5; that query is checking whether the term has any parents rather than the hierarchy of the vocab. I also noticed one other thing:
For these, should we avoid relying on the literal value of the disabled and multiple constants being less than the multiple? I think it might be better to check for:
Comment #7
chris.leversuch CreditAttribution: chris.leversuch commentedHere's a new patch based on #5 with the changes from #6
Comment #8
xjmHm, for these two, let's make the order of operations more explicit:
if (($vocabulary->hierarchy == TAXONOMY_HIERARCHY_DISABLED || $vocabulary->hierarchy == TAXONOMY_HIERARCHY_SINGLE) && count($tree) > 1)
Edit: Actually, how about this (not sure why I didn't think of this earlier):
if (($vocabulary->hierarchy != TAXONOMY_HIERARCHY_MULTIPLE) && (count($tree) > 1))
Comment #9
chris.leversuch CreditAttribution: chris.leversuch commentedChanged all 4 occurences from #6 to match the new logic in #8
Comment #10
xjmThanks @chris.leversuch. Codewise, the patch looks correct to me now. Two minor questions:
Sorry that I did not notice this before, but we need to move this comment up to the preceding line since it's now over 80 chars. I'd just reroll/RTBC with that change myself, except...
These two additions are new to the latest patch in #9. Is that intentional? They don't seem to be related to the issue.
Comment #11
chris.leversuch CreditAttribution: chris.leversuch commentedHa, oops. They're from #1370060: Add cross-references between taxonomy_vocabulary_load() and taxonomy_vocabulary_machine_name_load(). I obviously didn't have a clean working dir when I created the patch.
Try this one.
Comment #12
xjmAlright, this looks good now. Thanks @chris.leversuch!
Comment #13
catchLooks good. Committed/pushed to 8.x.
Comment #14
chris.leversuch CreditAttribution: chris.leversuch commentedHere's a D7 version
Comment #15
acouch CreditAttribution: acouch commentedI reviewed this and there are no instances of hierarchy that I could find that were missed.
Comment #16
webchickThis seems like a good improvement, but not sure it really makes sense for Drupal 7. It's not fixing a bug or adding a new capability, merely cleaning things up. If you feel strongly the opposite, we can discuss, though.
Moving back to 8.x and we should get a change notice for this.
Comment #17
izus CreditAttribution: izus commentedAdded change notice http://drupal.org/node/1385926
Comment #19
xjmComment #19.0
xjmUpdated issue summary.
Comment #20
Wim LeersFollow-up for this issue: #2807263: Impossible to write unit tests involving Vocabulary, because TAXONOMY_HIERARCHY_(DISABLED|SINGLE|MULTIPLE) are defined in taxonomy.module instead of VocabularyInterface.