For context, I'm integrating hierarchical selects with homebox module in a block settings form. The form uses hierarchical select as a FAPI element, and it works pretty well. However, once two blocks/forms are put on the same page I start having issues with the widget in the first form trying to operate the widget in the second form. This is because the two form elements share the same id, which is generated using _hs_process_determine_hsid. So:
The way _hs_process_determine_hsid currently assigns hsids doesn't work correctly when you have 2 copies of the same form, each containing a hierarchical select widget.
This is because $form_state['storage'] is not shared between the different forms.
This can be fixed by storing the hsid in $_SESSION instead of in $form_state, which looks like it was the say it was done in 6.x from the looks of <>.
Am I missing something here?
diff --git a/sites/all/modules/contrib/hierarchical_select/hierarchical_select.module b/sites/all/modules/contrib/hierarchic
index 731267d..4bd4fba 100644
--- a/sites/all/modules/contrib/hierarchical_select/hierarchical_select.module
+++ b/sites/all/modules/contrib/hierarchical_select/hierarchical_select.module
@@ -349,11 +349,11 @@ function _hs_process_determine_hsid($element, &$form_state) {
// generate a new one based on the last HSID used (which is
// stored in form state storage).
if (!isset($element['#value']) || !is_array($element['#value']) || !array_key_exists('hsid', $element['#value'])) {
- if (!isset($form_state['storage']['hs']['last_hsid'])) {
- $form_state['storage']['hs']['last_hsid'] = -1;
+ if (!isset($_SESSION['storage']['hs']['last_hsid'])) {
+ $_SESSION['storage']['hs']['last_hsid'] = -1;
}
- $form_state['storage']['hs']['last_hsid']++;
- $hsid = $form_state['storage']['hs']['last_hsid'];
+ $_SESSION['storage']['hs']['last_hsid']++;
+ $hsid = $_SESSION['storage']['hs']['last_hsid'];
}
else {
$hsid = check_plain($element['#value']['hsid']);
Comments
Comment #1
samhassell CreditAttribution: samhassell commentedOk here's a patch which makes a couple of changes allowing 2 things:
1) Get hs elements working correctly when multiple are loaded on one page via AJAX.
2) Port hs_taxonomy_term_count_nodes to Drupal 7.
I'm sure some improvements can be made to the patch, I'm not full bottle on how hs is working.
Sorry for combining what should be 2 patches :P
Comment #2
samhassell CreditAttribution: samhassell commentedLittle description of the changes:
When you have multiple hs elements, Drupal adds --1, --2, etc to the end of the #id. Updated to handle this, though it would be better to stop Drupal doing this in the first place?
This is the change outlined in the main issue.
This is outlined in the main issue
Without this we get back empty selects. Would be better handled inside _hierarchical_select_apply_entity_settings perhaps.
if special_items isn't set we get a warning.
if ..entity_count() returns empty terms, get rid of them. otherwise the element has an empty (well label only) child select.
avoid a warning
Port to D7 and DBTNG.
Comment #3
samhassell CreditAttribution: samhassell commentedFound some issues with the above patch stopping new taxonomy terms from appearing in the hierarchical select.
This new patch helps correct that, but it is still a bit buggy - top level terms with no children and no content will display an empty select box when selected.
Comment #4
samhassell CreditAttribution: samhassell commentedLooks like a lot of this patch made it into HS so I've stripped it down to just the hacky bit that needs work.
*edit* i was checking against code with the patch already applied :( -3 is still the latest.
Comment #5
samhassell CreditAttribution: samhassell commentedComment #6
stefan.r CreditAttribution: stefan.r commentedWe don't use this piece of code anymore :)