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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nkschaefer’s picture

OK. 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.

nkschaefer’s picture

Title: Change tree ID in Javascript to an array for greater flexibility » Send form parents to Javascript - crucial for Field API integration
nkschaefer’s picture

Status: Active » Needs review
mh86’s picture

Status: Needs review » Fixed

Just 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.

Status: Fixed » Closed (fixed)

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