I really like the form element provided by this module - my site makes use of the MeSH (Medical Subject Headings) vocabulary provided by NCBI/PubMed, which is a hierarchical vocabulary containing tens of thousands of terms, and the tree widget is a great way for users to choose terms.
The module I'm writing provides a form element that allows users to either choose terms with the Taxonomy manager tree, or to select using an autocomplete text field; it also handles its own multiple values and allows users to remove selected terms. As I'm working on integrating it with Field API, though, I've hit a problem related to how Taxonomy Manager keeps track of the widget's place in the form. Right now, the tree is given an ID, which consists of a string based on the element's #parents array (see line 343 in taxonomy_manager.module). This id is stored in Drupal.settings.taxonomytree. When a user expands a checkbox, an AJAX call is made -- it's handled by taxonomy_manager_tree_build_child_form() (line 858). The problem occurs in the function _taxonomy_manager_tree_sub_forms_set_parents() -- the offending line is 969, when the #parents array is rebuilt from the $tree_id string.
The problem stems from the fact that Field API fields often have underscore-separated names. In my case, the field is called "field_mesh." When the tree ID is created, this becomes "field-mesh," and later it's assumed that the hyphen separates parent elements, so the #parents array is set to something like
array(
'field',
'mesh',
'und',
0,
'value'
);
instead of
array(
'field_mesh',
'und',
0,
'value'
);
I'd propose, instead of creating a tree ID string, to just send the #parents array to Drupal.settings and POST it back -- or to use a different character (like |) to separate the elements of the array when it's made into the ID string. This would make the module more flexible and friendly with Field API.
I've never written a patch before, but I recently installed Git and everything, so I'll try to figure out how to do that and post the patch here.
Thanks,
Nathan
Comment | File | Size | Author |
---|---|---|---|
#1 | 1222214-taxonomy-manager-tree-form-parents-1.patch | 4.2 KB | nkschaefer |
Comments
Comment #1
nkschaefer CreditAttribution: nkschaefer commentedOK. As I said before, this is my first patch, so hopefully I did this correctly. I kept the tree ID in the settings, but added a new setting called "form_parents" (stored as a property of the tree object in JS called "formParents"), which is just a copy of the #parents array from the form API. This is sent with the other $_GET params in AJAX calls, and it's used now (instead of the tree ID) in the function _taxonomy_manager_tree_sub_forms_set_parents(). This allows my form widget (which integrates the taxonomy_manager_tree widget with Field API) to work.
As I said before, this seems like a crucial step to make it possible to integrate this widget with Field API, which should be a big priority.
Comment #2
nkschaefer CreditAttribution: nkschaefer commentedComment #3
nkschaefer CreditAttribution: nkschaefer commentedComment #4
mh86 CreditAttribution: mh86 commentedJust committed your patch, which really makes a lot of sense. Thanks!
One small note, your patch had a trailing whitespace at
+ new Drupal.TaxonomyManagerTree(treeSettings[i].id, treeSettings[i].vid, treeSettings[i].parents);
which should be avoided (you can see this with the right git config or the Dreditor). The Taxonomy Manager itself doesn't follow all coding standards yet, but I want to fix that soon.