When you place a hierarchical select inside a field collection and, during the content creation, you try to add a second field collection, the hierarchical select inside the first one lost the entered value.

Proposed resolution

The solution is to store in the $form_state['storage'] each element before form_hierarchical_select_process returns it using an array with keys composed using all the parents of the element.
When a key is present in the storage, form_hierarchical_select_process returns directly the stored element.
I've done all teh work in the patch attached.

Remaining tasks

The attched patch needs review and test in a lot of cases to be declared stable.
Storing all the element coud be determine a lack of performance, it could be usefull to store only significant part of the element in the $form_state['storage'] structure

Original report by dandaman

Issues when Hierarchecal Select is inside Field Collection

Members fund testing for the Drupal project. Drupal Association Learn more


tonyhibernator’s picture

I couldn't figure out how to apply this patch so posted for help here:

tonyhibernator’s picture

This patch works great for me, thanks.

TimLeytens’s picture

Patch seems to fix the issue


akamaus’s picture

Status: Needs review » Reviewed & tested by the community

Works for me too, I tested on two sites. The first one used HS in a field collection on a node creation form and on a second HS was placed on a heavily ajaxed entity creation form built using ECK. In both cases HS used for selectiong taxonomy terms.

You saved me a day, many thanks!

sabsbrain’s picture

Version: 7.x-3.0-alpha5 » 7.x-3.x-dev
Status: Reviewed & tested by the community » Needs review
2.08 KB

I have created a git patch for this for the latest DEV version of the module based on the above.

Hopefully this is a little easier to apply for people.

It seems to work fine for me.

lcampanis’s picture

Worked for me too :)

AlexFreem’s picture

Patch from post #5 works great! Thank you!

Davidz’s picture

It works for me. Thx

Quim’s picture

I'm using this patch in case of entity connect module. When I added some node by entity connect widget and got back to my basic content type's form I couldn't add any taxonomy using hierarchical select (browser freezes). Did you noticed this issue also?

thanks in advance for help.

Quim’s picture

That's wrong place I choosed
My issue has been solved by applying patch from this topic:

Thx and sorry for confusing post...

dandaman’s picture

Patch fixed my issue. Would be great if this could be incorporated into the official release.

Kars-T’s picture

Status: Needs review » Needs work
  1. +++ b/hierarchical_select.module
    @@ -358,6 +358,10 @@ function _hs_process_determine_hsid($element, &$form_state) {
    +  ¶

    Spaces behind line end.

  2. +++ b/hierarchical_select.module
    @@ -689,14 +693,19 @@ function _hs_process_render_nojs($element, $config) {
    + 	if(arg(0) != 'hierarchical_select_ajax') {  //@TODO: verify if we have AJAX call
    + 		$cid = isset($element['#parents']) ? implode("-",$element['#parents']) : implode("-",$element['#field_parents']); //Get unique identifier using parents of the field
    + 		$elhsid = drupal_array_get_nested_value($element, array('#value', 'hsid')); // verify if hsid is present or not
    + 		if (!isset($elhsid)) {
    + 			$cached = drupal_array_get_nested_value($form_state, array('storage', 'hs', 'hs_fields', $cid));	//retrieve previous element from form_state	¶
    + 		}
    + 		if (empty($cached))
    + 			$docache = TRUE;
    + 		else
    + 			return $cached;  //switch courrent element with the "cached" one))
    + 	}
    + 	// Determine the HSID.

    Please don't use tabs and no comments at line end.

  3. +++ b/hierarchical_select.module
    @@ -812,6 +821,9 @@ function form_hierarchical_select_process($element, &$form_state, $complete_form
    +  if (!empty($docache))
    +    $form_state['storage']['hs']['hs_fields'][$cid] = $element; //store new element in cache

    Please reread the coding and comment standards.

kirkofalltrades’s picture

I applied the patch in #5 and my issue with the Taxonomy getting reset on every instance inside the field collection was fixed. However, when I remove an entry that comes before another, I am left with a combination of the HS taxonomy field content and other fields that are part of the field collection.

My example. I have a 'Colleges Attended' field collection. College is selected with HS (State > City > College Name), then I collect start and end dates using the date field. Let's say I have 2 entries that already exist.

University 1, 9/2011-12/2012 and Univeristy 2, 2/2013 - 10/2013

And I remove the first University.

I am left with University 1, 2/2013 - 10/2013. As if I am getting the HS content of the instance I deleted, and the date content from the instance I actually wanted to keep.

Hoping for a quick response on this one. I'll likely be trying to find a way around it in the mean time.


andypost’s picture

Issue summary: View changes
Status: Needs work » Closed (duplicate)