We're using the Field Collection module to enable us to have groups of data that you can add more than one of to a node. However, the Hierarchical Select module doesn't seem to be compatible.

After adding a Hierarchical Select within a Field Collection, on the node creation screen the AJAX of the first select works pretty well, although there's a bunch of "You need to fill in this field" messages even though you really haven't even seen those fields yet. But the main problem is when you add another entry to the Field Collection, all of the Hierarchical Select values within that Field Collection are emptied out.

It seems to me that there's something within the code of Hierarchical Select that don't really take well to the form being rebuilt again before submission, is that correct? Is there anything that can be done to resolve this? I'm somewhat familiar with the D6 CCK system so with some hints as to where changes need to be made I may be able to tinker around and get things working. Thanks!

Comments

dandaman’s picture

Title: Issues when inside Field Collection » Issues when Hierarchecal Select is inside Field Collection

Updated title for clarity.

dandaman’s picture

It seem that, when you click the button to "Add another" field collection, it does send the data filled out by the Field Collection including the Hierarchical Select data, but the form_hierarchical_select_process() function is never executed. Thus, the Hierarchical Select fields are empty instead of displaying the data already entered. (I may be wrong about this; I'm just trying to figure out the issue myself.)

apmsooner’s picture

I concur. Same problem.

dandaman’s picture

This is definitely still a shortcoming that these two modules don't really work together. For now, I have created my own custom solution for getting around this. In other words, I created two separate fields for the hierarchical levels and added form validation and JavaScript to get it to do what Hierarchical Select used to do. So, personally, I have a custom solution for my basic use case, but this would be great to fix for other users in the long term.

filsterjisah’s picture

I also have this problem. I will also try a custom solution.

dmitry.n’s picture

I've rewritten the _hs_process_determine_hsid function to make it work until it is fixed by the maintainers:

function _hs_process_determine_hsid($element, &$form_state) {
  // Determine the HSID to use: either the existing one that is received, or
  // 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'])) {
    //we use field-collection's delta as hsid
    if ($element['#entity_type'] == 'field_collection_item' && isset($element['#parents'][2])) {
      $hsid = $element['#parents'][2];
    }
    else {//other cases
      if (!isset($form_state['storage']['hs']['last_hsid'])) {
        $form_state['storage']['hs']['last_hsid'] = -1;
      }
      $form_state['storage']['hs']['last_hsid']++;
      $hsid = $form_state['storage']['hs']['last_hsid'];
    }
  }
  else {
    $hsid = check_plain($element['#value']['hsid']);
  }

  return $hsid;
}

The issue with HS is that $hsid is calculated incorrectly when inside a field collection, so I'm using the field-collection's delta for a hsid

ironbuilt’s picture

Tried to implement your change and I don't notice a differenct. Are you just updating the function in the hierarchical_select.module file?

dmitry.n’s picture

yes, I've updated it. Have you tried to debug it? Did it go into

if ($element['#entity_type'] == 'field_collection_item' && isset($element['#parents'][2])) {
  $hsid = $element['#parents'][2];
}

condition?

di3gopa’s picture

I also tried it, and debug it,

$element['#parents'][2]; has a value, and it is getting inside the condition, but still erasing all of the hierarchical select fields when saving, here is my example -> http://www.youtube.com/watch?v=b5aKhVK9WmY

The bug is still happening

Thanks!

giupenni’s picture

Same problem.
I have this problem also if I use Field Group Multiple instead Field Collection.

giupenni’s picture

@dmitry.n :
I added your code but D7 put this error:

Notice: Undefined index: #entity_type in _hs_process_determine_hsid() (linea 354 di /media/dude/www/prova/sites/all/modules/hierarchical_select/hierarchical_select.module).

tonypaulbarker’s picture

Status: Active » Closed (duplicate)

This patch worked great for me. You may need to wikipedia diff to understand the syntax, I applied it manually.

http://drupal.org/node/1466516

alexander_danilenko’s picture

Yep, it works fine (:
"git patch" and "patch" are not worked for me. i have added manually code. and it start works (:

in attachment module (7.x-3.0-alpha5) what works fine if replace it (:

allabakash.g’s picture

Thanks, It resolved that issue.

Now we are facing another issue when I am trying to remove a field collection item from the node edit form, it is removing the field collection item and it shifts removed FC item's hierarchical select field values to succeeding field collection items.

Please check the attached screenshot images.

Example:
FC : Field Collection
FXX : Field's in field collection.
HS : Hierarchical Select

 FC1
    F11
    F12
    HS1
      
 FC2
    F21
    F22
    HS2

 FC3
    F31
    F32
    HS3 

 FC4
    F41
    F42
    HS4

Lets say i have removed Field Collection1(FC1), see the changed structure below

FC2
    F21
    F22
    HS1
 FC3
    F31
    F32
    HS2 
 FC4
    F41
    F42
    HS3 
senthil.na’s picture

This issue is resolved by adding below condition to the code drupal_array_set_nested_value($form_state['input'], $element['#array_parents'], array()) in form_hierarchical_select_process function.

function form_hierarchical_select_process($element, &$form_state, $complete_form) { 
  .
  .
  .
  // This prevents values from in $form_state['input'] to be used instead of
  // the generated default values (#default_value).
  // For example: $element['hierarchical_select']['selects']['0']['#default_value']
  // is set to 'label_0' after an "Add" operation. When $form_state['input']
  // is NOT erased, the corresponding value in $form_state['input'] will be
  // used instead of the default value that was set. This would result in
  // undesired behavior.
  if (empty($docache)) {  /* Condition */
    drupal_array_set_nested_value($form_state['input'], $element['#array_parents'], array());
  }
  .
  .
  .
}

Thanks

allabakash.g’s picture

Thanks devasena,
it was helpful!

senthil.na’s picture

StatusFileSize
new2.03 KB

I'm attaching updated patch file for hierarchical_select.module.

mavimo’s picture

Status: Closed (duplicate) » Reviewed & tested by the community
StatusFileSize
new1.9 KB

Rerolled patch to 7.x-3.x-dev

wim leers’s picture

Status: Reviewed & tested by the community » Needs work

This is not even close to RTBC, sorry.

  1. +++ b/hierarchical_select.module
    @@ -690,11 +690,25 @@ function _hs_process_render_nojs($element, $config) {
    +  // @TODO: verify if we have AJAX call.
    

    This cannot be RTBC if there's still a @todo

  2. +++ b/hierarchical_select.module
    @@ -690,11 +690,25 @@ function _hs_process_render_nojs($element, $config) {
    +    //Get unique identifier using parents of the field
    ...
    +    // verify if hsid is present or not.
    ...
    +      // switch courrent element with the "cached" one.
    

    Punctuation!

  3. +++ b/hierarchical_select.module
    @@ -690,11 +690,25 @@ function _hs_process_render_nojs($element, $config) {
    +    $elhsid = drupal_array_get_nested_value($element, array('#value', 'hsid'));
    

    s/$elhsid/$hsid/

andypost’s picture

Closed as duplicate #1466516: Error adding new field collection with Hierarchical select inside because the patches are the same

antonnavi’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new2.59 KB

Here is my implementation of this issue fix (patch is bit modified combination of existing patches).

andypost’s picture

Please re-roll to address minor nitpick, otherwise RTBC

+++ b/profiles/mcdonalds_testimonials/modules/contrib/hierarchical_select/hierarchical_select.module
@@ -359,6 +359,11 @@ function _hs_process_determine_hsid($element, &$form_state) {
+  ¶

@@ -807,8 +827,14 @@ function form_hierarchical_select_process($element, &$form_state, $complete_form
+  ¶

trailing white spaces

antonnavi’s picture

Trailing white spaces was removed

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Finnaly this works!

antonnavi’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new2.31 KB

Fixed path to module on patch.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Darn, path in the patch

kirkofalltrades’s picture

Patch in #25 seems to do the trick for me. My only remaining request would be that the HS field is reset (like other fields are) when you remove the last remaining item in a field collection.

Thanks!
Kirk

glajman’s picture

I have the same problem but using version alpha6.

Does anyone know if this can be patched for alpha6 too ?

Regards

-G

GBain22’s picture

Not working for me either - HS will not show the next dropdown after I choose a value - no errors thrown in the console - tried #25 but no difference

glajman’s picture

I am now using alpha5 but could not patch this correctly. Getting malformed patch error. Any help?

badalsolanki’s picture

#21 works fine...thanks

jon pugh’s picture

Re-rolled on dev snapshot.

I'm not sure this fixes it. I am still having problems, but my site has lots of those... ;)

jon pugh’s picture

Version: 7.x-3.0-alpha5 » 7.x-3.x-dev

  • stefan.r committed d070d00 on 7.x-3.x
    Issue #1349868 by Antonnavi, Jon Pugh, mavimo, devasena | dandaman:...
stefan.r’s picture

Status: Reviewed & tested by the community » Closed (fixed)

Committed, thanks!

stefan.r’s picture

Status: Closed (fixed) » Fixed

Status: Fixed » Closed (fixed)

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