Simple patch to get hierarchical_select fields to work as a controlling field.

Havent testet it much, but works for me.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

design.er’s picture

Thank you for this patch.
I've patched Conditional Fields 6.x-1.0-beta2 and use Hierarchical Select 6.x-3.x-dev (from 2009-Oct-02) with Content Taxonomy 6.x-1.0-rc2 on Drupal 6.14.
My approach is the following - that you better understand if I tested your patch correctly:
The user should be able to choose his/her relationship status like on facebook.
I've created a taxonomy vocabulary "relationship status" with following terms and nestings:
1. I'm single
2. I'm taken
- Is your partner registered on mysite.com? (Hierarchical Select label)
- yes
- no
3. I'm engaged
- Is your partner registered on mysite.com? (Hierarchical Select label)
- yes
- no
4. I'm married
- Is your partner registered on mysite.com? (Hierarchical Select label)
- yes
- no

Every term "yes" and "no" is the same term but only assigned to multiple parent terms (taken, engaged, married) - I hope this is not a problem in this case/for this patch.
I use Content Taxonomy (as CCK field) for this approach - Hierarchical Select works awesome with that and I hope this is not a problem in this case.

If user has chosen "yes" then a CCK userreference field shoud appear.
If users has chosen "no" then a CCK text field should appear.

My problem is that I can't see the Content Taxonomy Hierarchical Select field as Conditional field for the userreference and text field.
Sorry for this long post but I hope to explain every clear enough to help you with correct testing.

Kind regards,
Stefan

design.er’s picture

Status: Active » Needs review

I think this is the correct status.

design.er’s picture

Hello, any news? It would be great to get this functionality to work.

Bilmar’s picture

subscribing

peterpoe’s picture

Version: 6.x-1.0-beta2 » 6.x-2.x-dev
Status: Needs review » Needs work
design.er’s picture

Thanks a lot for your reply. I'll check the mentioned thread to stay updated.
Your idea with the plugin system sounds clever. I think this is the right way.

Bilmar’s picture

This patch seems to not work at the moment. Could someone kindly update it to work with the current dev?
I am needing to use the hierarchical select dropbox and have the selections control other fields.

I understand that further development should wait for #648934: Plugin system for compatibility with non-core CCK modules but it seems there has not been an update for two months and so until it is developed it would be awesome if the community has a way to utilize this great module =)

Compatibility with Content Taxonomy patch works and can be found at #250748: Integrate with Content Taxonomy

olemsa’s picture

FileSize
6.26 KB

Made a patch to conditional fields 6.x-2.x-dev

Seems to work fine with hierarchical_select 6.x-3.x-dev

Bilmar’s picture

Thank you for the patch. For testing I enabled hierarchical select dropbox feature and each choice is controlling a different field. What seems to happen is only the field controlled by the choice currently in the select list is showing. Adding choices into the hierarchical select dropbox doesn't have any effect on the controlled fields.

Steps to replicate:
1) field has choices A, B, C and set to hierarchical select dropbox (unlimited choices allowed)
2) on node form, having selection on choice A shows field controlled by choice A on the page
3) change selection to choice B shows field controlled by choice B (field controlled by field A disappears)
4) add choice B to the dropbox, and change select back to choice A. field controlled by choice B no longer shows and field controlled by choice A shows.

How I think conditional fields should work with dropbox feature.
1) select choice A, B or C => no fields controlled by A B or C should show yet on the page
2) add choice A to dropbox => after adding to the dropbox, fields controlled by choice A should show

Is it possible to correct this? Please let me know if more information is needed.
Thanks!

olemsa’s picture

The patch I made works fine for me, but I'm not using dropbox. The code needed to get CF to work with dropbox (as I see it) is more than a minor fix.

http://drupal.org/node/648934 <- I hope this plugin system is coming soon

Bilmar’s picture

Thanks olemsa-

Yes, I hope the plugin system #648934: Plugin system for compatibility with non-core CCK modules will develop soon with the help of the community. The patch seems to work when using regular HS to select one term, but does not work with dropbox when multiple terms are selected (it does not recognize items in the dropbox and acts to any term that is currently highlighted (in or out of dropbox)). It would be awesome if someone in the community can help with implementing conditional fields with HS dropbox.

Thank you very much in advance.

peterpoe’s picture

Status: Needs work » Active

I just committed a patch that makes Hierarchical select work as controlling field. Compatibility with the dropbox is partial: only items selected in the list are handled by conditional_fields.js, so when you add them to the box, cf can't do anything about it.

Bilmar’s picture

Thanks peterpoe!

Should this post be changed to a feature request with name "compatibility with hierarchical select dropbox"? or should I create a new feature request in the CF queue?

Is there any other way of allowing a user to select 5 terms from a list of say 50 terms and have the controlled fields of the 5 terms show, but presenting the 50 controller terms in a clean way? 50 checkboxes for the controller fields seems to take too much space at the top of the node form.

Thank you

YK85’s picture

I confirmed that HS dropbox is still not supported. Asking maintainer of HS for support.
Would anyone else be able to help out?
Thanks

Wim Leers’s picture

Does Conditional Fields work purely with JS (client side), purely through FAPI (server side), or a combination of both?

For JS; this is the HTML generated for a HS with a dropbox (from http://wimleers.com/demo/hierarchical-select/taxonomy):

<div class="form-item" id="edit-example-2-wrapper">
 <label for="edit-example-2">My car park: <span class="form-required" title="This field is required.">*</span></label>
 <div class=" hierarchical-select-wrapper hierarchical-select-level-labels-style-inversed" id="hierarchical-select-1-wrapper"><div class="hierarchical-select clear-block"><div class="selects"><select name="example_2[hierarchical_select][selects][0]" class="form-select" id="edit-example-2-hierarchical-select-selects-0"><option value="none" class="" selected="selected">&lt;none&gt;</option><option value="20" class="has-children">Aston Martin</option><option value="1" class="has-children">BMW</option><option value="9" class="has-children">Mercedes</option></select><div class="grippie"></div></div><input type="submit" name="op" id="edit-example-2-hierarchical-select-dropbox-add" value="Add" class="form-submit add-to-dropbox">
</div><input type="hidden" name="example_2[dropbox][hidden][lineages_selections][0]" id="edit-example-2-dropbox-hidden-lineages-selections-0" value="a:1:{i:0;s:2:&quot;21&quot;;}">
<input type="hidden" name="example_2[dropbox][hidden][lineages_selections][1]" id="edit-example-2-dropbox-hidden-lineages-selections-1" value="a:1:{i:0;s:1:&quot;6&quot;;}">
<div class="dropbox"><table><caption class="dropbox-title">Car park</caption><tbody><tr class="dropbox-entry first  odd"><td><span class="dropbox-item">Aston Martin</span><span class="hierarchical-select-item-separator">›</span><span class="dropbox-item dropbox-selected-item">Vanquish S</span></td><td class="dropbox-remove"><div class="form-item" id="edit-example-2-dropbox-visible-lineages-0-remove-wrapper" style="display: none; ">
 <label class="option" style="display: none; "><input type="checkbox" name="example_2[dropbox][visible][lineages][0][remove]" id="edit-example-2-dropbox-visible-lineages-0-remove" value="1" class="form-checkbox" style="display: none; "> Remove</label>
</div>
<a href="">
  Remove

</a></td></tr><tr class="dropbox-entry  last even"><td><span class="dropbox-item">BMW</span><span class="hierarchical-select-item-separator">›</span><span class="dropbox-item">7 series</span><span class="hierarchical-select-item-separator">›</span><span class="dropbox-item dropbox-selected-item">714i</span></td><td class="dropbox-remove"><div class="form-item" id="edit-example-2-dropbox-visible-lineages-1-remove-wrapper" style="display: none; ">
 <label class="option" style="display: none; "><input type="checkbox" name="example_2[dropbox][visible][lineages][1][remove]" id="edit-example-2-dropbox-visible-lineages-1-remove" value="1" class="form-checkbox" style="display: none; "> Remove</label>
</div>
<a href="">
  Remove

</a></td></tr></tbody></table></div></div>
 <div class="description">At least when I win the lottery … twice.</div>
</div>

The lineages in the dropbox are stored as hidden form input and the values are serialized PHP arrays:

<input type="hidden" name="example_2[dropbox][hidden][lineages_selections][0]" id="edit-example-2-dropbox-hidden-lineages-selections-0" value="a:1:{i:0;s:2:&quot;21&quot;;}">
<input type="hidden" name="example_2[dropbox][hidden][lineages_selections][1]" id="edit-example-2-dropbox-hidden-lineages-selections-1" value="a:1:{i:0;s:1:&quot;6&quot;;}">

On the server, you can find these arrays at $element['#value']['dropbox']['hidden']['lineages_selections']

peterpoe’s picture

Conditional Fields uses Forms api to generate the js settings, handle required fields and handle cases where the controlling field is not in the form. Js is used to show/hide the controlled fields depending on the value of the controlling fields. Since the drop box uses ahah, it would be quite complicated to make CF work fully with it.

robby.smith’s picture

+1 subscribing

Wim Leers’s picture

Okay, so Conditional Fields is unable to work with any complex AHAH-powered form element then?

Sounds stupid from an end user perspective, but sounds fair from a developer perspective. Forms API just isn't designed for these very advanced use cases …
I'm not sure if the amount of work will justify the gain in functionality? Maybe if some company steps up to sponsor it, but I know that I won't have time for it :(

theshanergy’s picture

subscribing

peterpoe’s picture

Status: Active » Fixed

I'm marking this as fixed, since compatibility with the dropbox is beyond the possibilities of the module at the moment. The rule to keep the module manageable since now has been not to include any module-specific code, but always try to keep it as general as possible. Compatibility with Content Profile and Content Taxonomy was obtained without breaking this rule, but I'm afraid it won't be possible with the dropbox.

Once #648934: Plugin system for compatibility with non-core CCK modules is in place (I'm working on it this moment) we could open another issue to add it as a plugin.

phantomvish’s picture

Sorry, it was the wrong thread, and I couldn't delete the posted comment.

Balbo’s picture

Do you thing is it possible to modify this patch to get hierarchical_select fields works as a CONTROLLED field?
Or should we wait for next release of the module?

phantomvish’s picture

+1 subscribing for HS as CONTROLLED field.

no2x’s picture

Status: Fixed » Needs work

Hi there,

i tested now conditional fields (both, 6.x-2.x-dev and 6.x-2.0-beta1) together with Hierarchical Select (6.x-3.1) and Content Taxonomy (6.x-1.0-rc2) and it does not work properly for me.

I have the following setting:

Content typ: accessories:
- content taxonomy field "category" using hierarchical select. The taxonomy tree looks like this:

+ printers
   + laser
   + inkjet
+ mass storage devices
   + hard disk drives
   + flash memory

- field "laser printer information" (Text field / checkboxes), controlled by field "category" with conditional fields module (shows only if laser printer selected).

Hierarchical Select settings for field "category" are: force to select deepest level, save only deepest level and no dropbox and no term creation allowed.

The problem is:
When a content is created and printers/laser is selected, the field "laser printer information" is not showed. If the hierarchical select category is changed AFTERWARDS to anything else, the field is displayed then (even though it should only display on printers/laser). If the category is changed again, the field is hidden again.

Does anyone have the same problem?

EndEd’s picture

+1 for HS as controlled

EndEd’s picture

Plz, Could anyone tell me if HS as a controlled field is more dificult to get done in a patch than a Controlling field as the #8 patch ?
Also, is there a way to modify the #8 patch to get this kind of behavior (HS as a controlled field) ?
Thanks

momper’s picture

subscribe

mirzu’s picture

I was able to modify the patch from above to work with the latest release attaching a new patch. I can confirm that this works with a hierarchical select enabled content taxonomy field that is controlling text and select fields. This patch would need to be cleaned up before it is rolled into the module, and I would be happy to do so if the maintainers are going to move in this direction.

g.k’s picture

subscribe

akin.demirci’s picture

The #28 patch worked for me. I'm using hierarchical select in a content taxonomy field without dropbox.
THanks...

Aron Novak’s picture

Mostly works, but not perfectly.
I found the following bug:
Term hierarchy:
T1 - T2
T3

all of these terms above triggers a different textfield in my configuration.
If i select T1 then T2, the proper fields appear.
If i select T3, the proper field appears, BUT the field for T2 remains displayed, which is not proper behaviour. I'm sure the JS of the module can be modified to take care of the HS widget's specialities.

Aron Novak’s picture

Status: Needs work » Needs review
FileSize
7.04 KB

I re-rolled #28 and included some JS changes to only take care of a value when the form item is visible.
At this case, HS hides the not active selector, but not removes it. So the value from it is extracted by conditional fields.
Dropbox is still not covered.

Wim Leers’s picture

Instead of supporting HS' dropbox functionality itself directly, you may want to enable HS' "render_flat_select" setting. This will result in an additional normal (yet hidden) select to be rendered with in there only the selected items in the hierarchy. That may be very handy…

Aron Novak’s picture

@Wim Leers Thanks for the suggestion, seems to be a good way. However it does not solve all the problems, for example the Add and Remove operations has to be handled from Conditional Fields to support dropbox-style even if render_flat_select is turned on. I tried to implement a way, but no success yet.

Wim Leers’s picture

Okay, then it's a bitch to support, I think.

I could add additional events to HS if that'd help. (See API.txt's "JavaScript events" section.)

drupalnesia’s picture

Version: 6.x-2.x-dev » 6.x-2.0-beta2
FileSize
59.02 KB
5.26 KB

These files contain both path #32 + path #18 from http://drupal.org/node/619290
#32: fix HS issue
#18 from http://drupal.org/node/619290: fix Content Profile issue

Both applied to CF 6.x-0-beta2, just replace with these files included (you must rename to conditional_fields.js and conditional_fields.module).

drupalnesia’s picture

@Aron Novak:
I see:

@@ -691,7 +709,11 @@ function conditional_fields_node_after_b
+	  $form[$controlled_field_name]["#field_name"]=$form[$controlled_field_name]["#parents"][0];drupal_set_message("ee");

Why you add drupal_set_message("ee"); ?

Attached file with drupal_set_message(""); only.

EDIT: please read #38 before use this attachment.

drupalnesia’s picture

Latest patch (#32) doesn't work if using CCK + HS and save the value only to CCK field (not saving to node term). The patch only detect tids while the value will be saved to field_cck_field_name, thus make inconsistence, you can not use field_cck_name[0][value] when validate a node. Anyone experience same issue?

This won't work

function yourmodule_nodeapi(&$node, $op) {
  if ($op =='validate'  && $node->type == 'test') {
    // not work if CF enable! because $node->field_test_1[tids]
    // works fine if CF disable
    if (!$node->field_test_1[0][value]) {   
        .....
        .....
    } 
  }
}

EDIT: Also not work if a HS-field set to "Required", always error "Xyz field is required"

Encarte’s picture

subscribing

ionmedia’s picture

Version: 6.x-2.0-beta2 » 6.x-2.x-dev

in my case HS controll many other cck fields and it is not working at all

loze’s picture

I have noticed that if HS is NOT set to save the term linage (save only deepest term) and the triggering value is a parent of the selected term, the controlled field is triggered (shown int he form) but no data is saved.

for example:
HS tree is a controlling field and something like: term A > term B > Term C
where term A is the triggering value for a cck field.

When the node is saved, the value of the textfield is not saved since only "term C" from the HS field is being saved and it is not a triggering value, although it is a child of the triggering value.

The temporary solution is to either have the HS field set to save the linage, or select every potential deepest level value as the triggering field.

I think CF should check if a parent value is set in the form, and save the triggered fields value.

BeaPower’s picture

Does this work with drupal 7?

amogiz’s picture

Subscribing for D7 !

pgrond’s picture

It does not work yet for Drupal 7 as far as I know. Also see this issue http://drupal.org/node/1325538

fiveaces’s picture

Subscribing for D7

ijujym’s picture

Subscribing D7

marcoBauli’s picture

Status: Needs review » Needs work

D7 users should watch http://drupal.org/node/1325538 as pgrond points out. Seems patches above are no more compatible with latest versions of Conditional Fields and HS.

Helmut Neubauer’s picture

subscribe D6

Helmut Neubauer’s picture

subscribe D6 - Patch of #36 works for me (concerning the compatibility to Hierarchical Select). Is ist possible to add the changes to the dev version (to have them for the next update?) and to change the code according to #38 ?

peterpoe’s picture

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