See the API: http://api.drupal.org/api/function/taxonomy_field_info/7

Taxonomy term reference field allowed values is an array of trees or subtrees. When the 'parent' is 0, it means to use the whole vocabulary. The 'vid' setting would ideally be the vocabulary's machine name. Or not? Please advise.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yched’s picture

Priority: Normal » Major

Indeed, that's an oversight. As is, the much-awaited introduction of vocab_machine_name doesn't bring any actual progress in terms of exportability (well, the initial reason for the introduction of vocab_machine_name was not exportability but simply to provide 'bundles' in order for terms to be fieldable - but it would be a shame to not take the exportability benefits that come along).

Thus, raising to 'major'.

Although : vocab machine name is editable and thus can change over time. When it does, we need to update all existing $field definitions and change machine names accordingly.

yched’s picture

Title: Taxonomy term reference field allowed value setting for vocabulary is stored as DB primary key instead of machine name. » Exportability of vocabularies is runined by taxo field 'allowed vocabs' settings

Less techie (?) title

fago’s picture

Subscribing.

yhahn’s picture

The following patch switches the reference of taxonomy term reference fields to use the vocabulary machine_name rather than vid. The effectiveness of this patch for exporting term reference fields and vocabularies has been tested using Features 7.x.

yhahn’s picture

Status: Active » Needs review
andypost’s picture

Suppose 'forum' module should be fixed too...

Status: Needs review » Needs work

The last submitted patch, 881530_vocabulary_field_machine_names.1.patch, failed testing.

yhahn’s picture

Status: Needs work » Needs review
FileSize
6.49 KB

Updated patch fixes Forum module field creation. 16 assertions (breadcrumb related) fail locally in the forum module tests with or without the patch.

Anonymous’s picture

Status: Needs review » Needs work

Thanks for picking this up yhahn.

Unfortunately, my Dreditor is broken, so we'll just have to do it in Drupal 6 fashion.

Index: modules/taxonomy/taxonomy.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.module,v
retrieving revision 1.605
diff -u -p -r1.605 taxonomy.module
--- modules/taxonomy/taxonomy.module	11 Sep 2010 06:03:12 -0000	1.605
+++ modules/taxonomy/taxonomy.module	21 Sep 2010 22:50:33 -0000
@@ -1075,7 +1075,7 @@ function taxonomy_field_info() {
       'settings' => array(
         'allowed_values' => array(
           array(
-            'vid' => '0',
+            'vocabulary' => '0',
             'parent' => '0',
           ),
         ),

Is 0 appropriate? It's never a valid machine name. It's probably fine, but I suspect this value is the original value for that setting for new fields.

The comment on taxonomy_field_info() needs to be updated to reflect the change from vid to vocabulary machine name.

Will this need an update hook? Or is this something for head2head to deal with?

sun’s picture

Title: Exportability of vocabularies is runined by taxo field 'allowed vocabs' settings » Exportability of vocabularies is ruined by taxonomy field's 'allowed values' setting
+++ modules/taxonomy/taxonomy.module	21 Sep 2010 22:50:33 -0000
@@ -1075,7 +1075,7 @@ function taxonomy_field_info() {
-            'vid' => '0',
+            'vocabulary' => '0',

I agree, the default should be an empty string.

+++ modules/taxonomy/taxonomy.module	21 Sep 2010 22:50:33 -0000
@@ -1259,10 +1259,12 @@ function taxonomy_field_formatter_view($
+      $terms = taxonomy_get_tree($vocabulary->vid, $tree['parent']);
+      if ($terms) {

We can put the first line into the condition here.

+++ modules/taxonomy/taxonomy.module	21 Sep 2010 22:50:33 -0000
@@ -1361,9 +1363,11 @@ function taxonomy_autocomplete_validate(
     foreach ($field['settings']['allowed_values'] as $tree) {
-      $vids[] = $tree['vid'];
+      if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
+        $vocabularies[$vocabulary->vid] = $vocabulary;
+      }
     }

I wonder why this is still based/keyed by vid? It would make more sense to consistently use the vocabulary machine name.

+++ modules/taxonomy/taxonomy.module	21 Sep 2010 22:50:33 -0000
@@ -1371,14 +1375,14 @@ function taxonomy_autocomplete_validate(
-        $vocabulary = taxonomy_vocabulary_load($vids[0]);
+        $vocabulary = reset($vocabularies);
         $term = array(
           'tid' => 'autocreate',
-          'vid' => $vids[0],
+          'vid' => $vocabulary->vid,
           'name' => $typed_term,
           'vocabulary_machine_name' => $vocabulary->machine_name,
         );

I wonder whether we cannot simply drop the entire 'vid' line? A machine name should be sufficient to store a term -- it's even "more unique" than the vid after all.

Powered by Dreditor.

yhahn’s picture

Status: Needs work » Needs review
FileSize
8.25 KB

Latest version.

  • Updates comment for hook_field_info()
  • Default vocabulary in settings is empty string
  • $terms = taxonomy_get_tree(...) moved into if()
  • Update 7009 added to taxonomy.install for updating field definitions from old to new format

The latter instances of vid that sun notes are necessary as they are used to load and return term objects. Term objects still need to be loaded using vid when using a condition with taxonomy_term_load_multiple() as {taxonomy_term_data} references vocabularies by vid. I think it is fine to use vid for internal/joining usage but in stored settings and external API calls moving to using the vocab machine name will be best.

Status: Needs review » Needs work

The last submitted patch, 881530_vocabulary_field_machine_names.3.patch, failed testing.

yched’s picture

Thanks yhahn for tackling this.

The update function belongs in head2head rather than core. The current D6->D7 update func that creates taxo fields from D6 vocabs should probably be amended though ?

moshe weitzman’s picture

Awesome to see work on this. Subscribe.

yhahn’s picture

Status: Needs work » Needs review
FileSize
8.09 KB

v4:

Status: Needs review » Needs work
Issue tags: -export

The last submitted patch, 881530_vocabulary_field_machine_names.4.patch, failed testing.

Anonymous’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
Issue tags: +export

The last submitted patch, 881530_vocabulary_field_machine_names.4.patch, failed testing.

yhahn’s picture

Status: Needs work » Needs review
FileSize
9.7 KB

Updated, fixes upgrade path test.

Status: Needs review » Needs work
Issue tags: -export

The last submitted patch, 881530_vocabulary_field_machine_names.5.patch, failed testing.

yhahn’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
Issue tags: +export

The last submitted patch, 881530_vocabulary_field_machine_names.5.patch, failed testing.

yhahn’s picture

Status: Needs work » Needs review
FileSize
10.41 KB

Found one more spot that needed fixing.

arianek’s picture

subscribe

chrisshattuck’s picture

I may not be the best one to review this patch, but from what I can tell it looks good. I haven't had a chance to run the patch myself, but I'd really like to see the machine names leveraged for real exportables in core wherever we can.

jhedstrom’s picture

subscribing

eaton’s picture

Status: Needs review » Reviewed & tested by the community

I just took this through its paces, from running the tests to poking through the UX to experimenting with a bit of sample code to create and manipulate vocabularies. The code changes are simple and straightforward, and the fix facilitates a really, really essential part of D7's deployability. Big +1 to the patch.

Anonymous’s picture

Thanks Eaton for testing it so thoroughly. I concur on RTBC.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

I think this patch makes sense.

Machine names for vocabulary names went in a long time ago, with the expectation that they'd work for feature exportability. This patch fixes it so it does. If it broke anything too badly, our tests should be screaming now.

Committed to HEAD. Thanks!

alex_b’s picture

w00t! :)

catch’s picture

Priority: Major » Normal
Status: Fixed » Needs work
Issue tags: +Needs documentation, +API change

This should be documented as an API change (for any modules or install profiles that create term reference fields).

Added a head2head issue - http://drupal.org/node/928278

irakli’s picture

+1 w00t!

yched’s picture

yay, thx yhahn !

@catch #31 : D7/D7 API changes are usually advertised on the dev mailing list, not on the D6/D7 migration page per se.
http://drupal.org/node/224333 did contain some sample code that needed to be updated accordingly, I just fixed that - see
http://drupal.org/node/224333/revisions/view/1179662/1181260 for the changes.

Anything else before turning back to 'fixed' ?

yched’s picture

See also #876762: Modules have no way of knowing if vocabulary machine names are changed for another important block on the road to vocab exportability.

catch’s picture

yhahn posted a head2head issue which I committed, that plus the docs changes seems fine to me, moving back to fixed.

catch’s picture

Status: Needs work » Fixed
rfay’s picture

OK, so please explain the API change to me :-) Thanks.

catch’s picture

The API change is this:

1. Any module or install profile using field_create_field() to create a term reference field will need to specify a vocabulary machine name for allowed values instead of $vid.
2. (just found this out when merging in head for ex2), any term reference validation handlers or other contrib or custom widget code which checks allowed values will need to change from $vid to machine name too.

Status: Fixed » Closed (fixed)
Issue tags: -export, -Needs documentation, -API change

Automatically closed -- issue fixed for 2 weeks with no activity.