Problem
Within the javascript method updateFieldValue the value of the hidden text field is set by jquery's val() method.
Unfortunately no change event is dispatched by this method, so the States API won't recognize the changed value.

Probably #2144869: Simple hierarchical select and conditional field are not working together? has something to do with that too, but since they strongly focus on the Conditional Fields module, I opened this new issue.

Solution
Trigger the change event manually.

Steps to reproduce
1.) Create a node type using a text field and a taxonomy field using shs.
2.) Add the following state to the text field:

array(
  '#states' => array(
    'enabled' => array(
      'css_selector_matching_the_hidden_select_field' => array('value' => '1'),
    ),
  ),
);

3.) Now the text field should be disabled unless the taxonomy term #1 is selected in the shs field. But it stays disabled, even if the term #1 is selected.

Comments

Tobias Xy’s picture

Status: Active » Needs review
StatusFileSize
new226 bytes

Created a patch.

arosboro’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new226 bytes

Patch didn't apply cleanly for me. Re-rolled it against the latest dev branch, and it works with the states api as described.

arosboro’s picture

The patch I submitted is identical to the author's. What I didn't realize is that I was applying it to the non dev release of this module. In this case you need to take care that the change event gets added to the correct javascript function.

simeonkesmev’s picture

Works for me.

sgdev’s picture

I'd appreciate any feedback on the patch I've created that extends what's included in this patch: updateFieldValue doesn't dispatch an accessible change event

Thanks.

l0ke’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new269 bytes
new194 bytes
+++ b/js/shs.js
@@ -475,6 +475,7 @@
+      $field_orig.trigger('change');

Change event triggered only when we going deeper in hierarchy. This means if for example we had some value selected and now we want to select "_none" change event won't be triggered.
I moved trigger out of conditional state to call it in all cases.

Mirkozzo’s picture

I did not understand what patches to apply on which version, i tried to add the line n°542 to shs.js 7.x-1.6 and 7.x-1.6+56-dev

539       }
540      }
541    }
542	$field_orig.trigger('change');
543  }

but it had no effect.

I urgently have #states working with shs module, or other system to dynamically use the terms of taxonomy from shs widget

l0ke’s picture

Patch made for dev version, and worked fine for me.
Can you please provide code listing of form, so I can take a detailed look.

Mirkozzo’s picture

StatusFileSize
new20.73 KB

I attached my shs.js (7.x-1.6+56-dev) modified

function mymodule_form_alter (&$form, &$form_state, $form_id) {
$form['field_my_text_field']['#states'] = array(
		'visible' => array(
			':input[name="field_my_taxonomy_field[und]"]' => array('value' => (string) '168'),	//TID:168
			),
		);
	}
}

but it had no effect.

If i use default taxonomy drupal widget -> work
If i use shs widget -> not work (field_my_field_text is always shown)

That is highly urgent for me :/

sgdev’s picture

@Mirkozzo, I understand you're trying to get things to work, but we're all dealing with highly urgent problems. What you're requesting is a support issue, not a problem with the patch. I'd like to make two suggestions:

1) Create a new ticket as a support request and see if others can provide some assistance with your issue.

2) Spend some time reviewing the module APIs in the shs.api.php file. This will provide additional details on how the widget can be hooked and altered to achieve your goals.

Mirkozzo’s picture

Tnx for help me

I found the error

[...]
':input[name="field_my_taxonomy_field[und][0][tid]"]' => array('value' => 'tid_number'),
[...]

I have modify shs.js of stable release 1.6....work fine

stborchert’s picture

Status: Needs review » Fixed

This has been fixed recently.

sgdev’s picture

@stBorchert, we tested this extensively, and found that the change trigger needed to be one level higher in the nested conditionals. If it isn't moved up, there are some cases where it doesn't work.

I've created an updated patch (https://www.drupal.org/node/2499279#comment-10917127) that includes a feature to allow an accessible change event. I'd appreciate if you can review it. Thanks.

Status: Fixed » Closed (fixed)

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