// @todo: I have no idea how to make this work if the triggering field affects
// more than one option limited field!

This case, it'd be nice to support. Currently it only changes one field even though allows configuring it.

Comments

joachim’s picture

Thanks! Patch works nicely if I add, say, two city fields both limited by the country field.

There's just a bit of tidying up I'd like:

+++ b/reference_option_limit.module
@@ -269,7 +269,7 @@ function reference_option_limit_form_alter(&$form, &$form_state, $form_id) {
+      $settings['ajax_wrapper'] = $form[$field_name_option_limited]['#reference_option_limit_id'] = 'reference-options-limit-' . str_replace('_', '-', $field_name_option_limited);

I'm not too keen on a triple assignment, especially when it's such a long line! Can we build an $reference_option_limit_id variable first, then have two assignments?

+++ b/reference_option_limit.module
@@ -449,21 +449,22 @@ function reference_option_limit_js($form, $form_state) {
+      $return['#commands'][] = ajax_command_replace('#' . $form[$settings['field']]['#reference_option_limit_id'], $message_html . render($form[$settings['field']]));

I'd rather build up the parameters to the call as variables first. Again, it's a long line, and like this it's hard to see where one param ends and one starts.

This would be clearer I think:

      $selector = '#' . $form[$settings['field']]['#reference_option_limit_id'];
      $html = $message_html . render($form[$settings['field']]);
      $return['#commands'][] = ajax_command_replace($selector, $html);
+++ b/reference_option_limit.module
@@ -449,21 +449,22 @@ function reference_option_limit_js($form, $form_state) {
+      $message_html = '';

I see what we're doing here -- only outputting the messages, if there are any, in front of the first ajax replacement.

I'm wondering though if there's a more graceful way to do it -- but not coming up with anything yet! If we have to do it this way, could we have a comment to explain what we're doing these funny manoeuvres for?

hefox’s picture

StatusFileSize
new3.4 KB

This should address the messages part. Instead of appending it, adds a div above the triggering field and adds any messages to it. Could also do it below field, not sure which is better usability wise.

hefox’s picture

StatusFileSize
new3.29 KB

unintentionally changed a line

pomliane’s picture

Status: Needs review » Reviewed & tested by the community

Works as expected, thanks hefox!

joachim’s picture

Status: Reviewed & tested by the community » Needs work

Still needs some of the tidying up mentioned in #1.

A few more things too...

+++ b/reference_option_limit.module
@@ -288,6 +288,16 @@ function reference_option_limit_form_alter(&$form, &$form_state, $form_id) {
+        // add a div to put any messages. ¶

Comments should be a full sentence with a capital. Trim whitespace here too.

+++ b/reference_option_limit.module
@@ -288,6 +288,16 @@ function reference_option_limit_form_alter(&$form, &$form_state, $form_id) {
+          '#tag' => 'div',
...
+          '#type' => 'html_tag',

These are not documented in https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.h... -- what are they?

+++ b/reference_option_limit.module
@@ -449,21 +459,20 @@ function reference_option_limit_js($form, $form_state) {
+  $return = array(
+    '#type' => 'ajax',
+    '#commands' => array(),
+  );

A comment here to say we're building an array of ajax commands, plus a @see to https://api.drupal.org/api/drupal/includes!ajax.inc/group/ajax_commands/7 would help future maintainability.

+++ b/reference_option_limit.module
@@ -449,21 +459,20 @@ function reference_option_limit_js($form, $form_state) {
+    $return['#commands'][] = ajax_command_replace('#' . $form[$field_name_triggering]['messages']['#attributes']['id'], '<div class="views-messages">' . $messages . '</div>');

This could do with a mention that we're outputting the messages on the triggering element, as we can't know what changing element they refer to.

mazdakaps’s picture

Hello
tried that patch above and its working . Three field one dependent to the other. Like that:
Country
Region
Town

Region dependent to Country and Town dependent to Region. When i select the Country field it limits as it should the Region field
. Also the Region field when i select a Region limits the Town field. But if i change my mind and want to select another Country it only populates the Region field but not the Town field. I could make both fields dependet to Country field but it would be great when first field changes both fields could change also not only the one.
Thank you very much

ryanoreilly’s picture

Confirming patch in #3 works.

nwom’s picture

#3 worked for me as well. Thanks!

amorales@drupal.org.es’s picture

#3 worked for me.... But not for required fields.

Country --> required
Region --> Not required
Town --> Not required

Any idea for solving this?

Thanks!

nwom’s picture

@amorales All of my fields are required. Are you sure that the module works with just one field for you?

amorales@drupal.org.es’s picture

Sorry, the module works for me the same way before and after aplying the patch

Applying the patch over 7.x-1.4 displays the following message:

patching file reference_option_limit.module
Hunk #2 succeeded at 299 (offset 29 lines).
Hunk #3 succeeded at 317 (offset 29 lines).
Hunk #4 succeeded at 488 (offset 29 lines).

Checked then the .module file and all changes were made right.

My configuration is as follow:
Taxonomies:
3 Taxonomies: Region --> City --> Area
City has one term reference: Region
Area has two reference terms: Region and City. City is checked with "Limit this field's options according to matching field values" and matching field with the region field Hiding options...

Content Type:
Added reference terms fields for Region, City and Area.
City and Area have the option for limiting the field checked, hiding option is also checked. Area field is limited with City.

Behavior:
If both 3 fields, Region, City and Area are required fields:
If I select RegionA then automatically appears CityA selected as the first option for that region, but AreaA is not displayed. I have to change to other city and select back again to CityA, then AreaA is displayed correctly. It's like being hung until I select the city field again.

If City is not required field but Region and Area are:
I select RegionA, then in the city box I have None and the cities for that region. Then when I select CityA the Area's box is updated with the areas for that city.

mpotter’s picture

Status: Needs work » Needs review
StatusFileSize
new3.63 KB

OK, here is an updated version of the patch applied to the latest -dev.

I tried to incorporate the requested comments, but really, we shouldn't need to teach people how to use ajax commands here. Let's focus less on exact comment wording and get this committed. The comments can always be improved in the future, but let's not take months to make these kind of improvements.

Using this patch in Open Atrium 2 along with #1986532: Support organic groups OG reference widget.

mas0h’s picture

The same thing with #6 happens with me. If I decide to change the region I get an error message "An illegal choice has been detected" with views.

pmichelazzo’s picture

Working like a charm!

Thank you for the patch

maxmendez’s picture

Thanks mpotter, the patch #12 work perfectly

jonhattan’s picture

#6,#11,#13 you want #1809338: Three fields in chain?. This issue is about two fields (second level) controlled by the same one, as in:

               |-> origin city
Country--------
               |-> destination city
jonhattan’s picture

Issue summary: View changes
StatusFileSize
new3.67 KB

Hell, I was to mark this RTBC, but fixed coding standards in comments. I think all concerns in #1 are addressed now.

Status: Needs review » Needs work

The last submitted patch, 17: 1986526_reference_option_limit_17.patch, failed testing.

The last submitted patch, 17: 1986526_reference_option_limit_17.patch, failed testing.

alauddin’s picture

#17 patch failed...got white screen of death.

Tested #12 patch and it works...ajax option limit filters both fields B & C referencing to A.

Thank you.

esolano’s picture

Assigned: Unassigned » esolano
Status: Needs work » Needs review
StatusFileSize
new4.54 KB

Hi all,

I tried patch in #12 and it works when you have several fields referencing the same one, like A -> D, B -> D, C -> D.
But when you have fields referencing others, like in a chain, it fails. Like A -> B, B -> C, C -> D.

I took the patch in #12 and made it work for both scenarios. It works for me. If someone could give it try, and add some feedback, that would be nice.

This does not fix the "An illegal choice has been detected" issue when the fields are required, and there are no options for available for one of the referenced fields.

Regards!

Status: Needs review » Needs work

The last submitted patch, 21: 1986526_reference_option_limit_21.patch, failed testing.

kaushashah’s picture

Hello,

I applied the patch mentioned in #12 and it worked like charm. The only problem I am facing is when I save the node, both the field values are not getting saved up, only one of them is getting saved. Any help in this is appreciable.

kaushashah’s picture

It got solved!!! Tried it out with fresh db and it worked. It now saves both the field value with #12 patch. Thanks mpotter

johnpitcairn’s picture

Patch at #12 applies against latest dev (2014-Aug-10) with Hunk #4 succeeded at 521 (offset 29 lines).

Works for me, with a master taxonomy reference field triggering changes in one entityreference field and one taxonomy reference field.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 21: 1986526_reference_option_limit_21.patch, failed testing.

The last submitted patch, 3: 1986526_reference_option_limit_3.patch, failed testing.

The last submitted patch, 12: 1986526_reference_option_limit_12.patch, failed testing.

The last submitted patch, reference_option_limit_multiple_changed_fields.patch, failed testing.

The last submitted patch, 2: 1986526_reference_option_limit_2.patch, failed testing.

johnpitcairn’s picture

johnpitcairn’s picture

Status: Needs work » Needs review
StatusFileSize
new4.47 KB

@joachim & @esolano: looks patch #21 was created from the parent directory. Let's try this one, otherwise unmodified and applies locally for me.

Status: Needs review » Needs work

The last submitted patch, 33: reference_option_limit-1986526-33.patch, failed testing.

johnpitcairn’s picture

Works for me but looks like the tests might need updating for this?

koppie’s picture

Looks like there were some major code changes between 1.4 and 1.5, and more with the dev version released in February. In particular looks like most of the actual ajax work was taken out of reference_option_limit_js().

Does anyone feel like re-rolling the patch against 1.5 or the latest dev? I took a hack at it but couldn't figure out where to put all the ajax stuff.

brunorios1’s picture

As @koppie said, patches doesn't applies anymore.

tested #12 and #33 in the last dev.

dariogcode’s picture

I have the same case described: Like A -> B, B -> C, C -> D.

And couldn't apply this patch agains the latest dev version. It appear the module doesn't use form alter anymore then I lost the path where to put the changes.

Any help is appreciated

thanks!

xiaodoudou’s picture

Status: Needs work » Needs review
StatusFileSize
new3.07 KB

I did quickly fix the patch, but test are needed to confirm this one could work in all case.

I did the following case:
A --> B
A --> C

Let me know if it does cover all cases.

Status: Needs review » Needs work

The last submitted patch, 39: reference_option_limit-1986526-34.patch, failed testing.

xiaodoudou’s picture

StatusFileSize
new3.29 KB

Did fix my patch to use git format and proper names.

xiaodoudou’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 41: reference_option_limit-1986526-34.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 41: reference_option_limit-1986526-34.patch, failed testing.

Vector-’s picture

Marking duplicate:
#2444851: When multiple fields source from same field, error 500 is returned in AJAX.

I liked the approach taken by the patch from @joonas.lahtinen in the above issue better than the one here, so I rerolled it with some cleanup on top of #1986532: Support organic groups OG reference widget, which seems to cover a lot of issues that might make implementing it troublesome otherwise.

This is actually the interdiff, not a proper patch, sorry.

Vector-’s picture

Rebased my above patch on dev and rerolled with #1986532: Support organic groups OG reference widget applied so we can queue it for testing.

Interdiff is poorly named above.

Vector-’s picture

This version should pass existing tests.

And the interdiff against #1986532-24: Support organic groups OG reference widget.

Doesn't work.

Vector-’s picture

Status: Needs work » Needs review

Can we set this to Needs Review now that existing tests work, or do we need new tests too?

Vector-’s picture

That last patch missed some cleanup during the reroll.

Sorry about the patch spam.

Doesn't work.

Vector-’s picture

Nope. This needs work still.

#1986526-47: Support one field changing 2+ fields works for me, but fails tests.

Can't seem to get the ones that pass testing to work in practice, not sure where things are going wrong here.

Vector-’s picture

This patch requires #1986532-24: Support organic groups OG reference widget. Likely still fails tests, but might work a bit more reliably?

I added in the recursive methodology found here to better handle dependencies, and added a fix for an issue raised in #1809338: Three fields in chain?: fields being processed by ID. Since we're doing a recursive dependency check, I used the resultant stack to rebuild a processing chain that should (I think?) respect those dependencies while processing fields.

There's no circular checking so... yeah, don't do that.

PS: This still needs some cleanup - things like passing $form_state by reference for no particular reason (stuff leftover from a previous iteration of the code), replacing the trigger check with a call to add_dependencies and refactor add_dependencies to return a $match variable.

This definitely needs tests too.

Vector-’s picture

Quick update: I did a bit of digging and found that checkboxes have special handling in the tests, and it's checkboxes that are failing tests with the patch.

Also, if we hack it to work by adding back the 'wrapper' to the ajax elements and using ajax_command_insert(NULL, ...) on the first element, and ajax_command_replace on the rest, those tests should pass - or at least they do for me locally. I don't think that's the right way to fix this though - especially with what I'm doing in #53 that would be fragile at best.

My guess is using replace instead of insert is breaking the special condition handling in those checkbox tests somehow, but that's as far as I've tracked it so far, and I probably won't have time to come back to this for a few weeks at least.

Vector-’s picture

Here's the cleaned up version at least with the changes I mentioned in #53.
Still requires #1986532: Support organic groups OG reference widget.

Vector-’s picture

I feel bad for filling this thread with junk as I fail at rerolling things correctly.
Depends on #1986532-24: Support organic groups OG reference widget

Instructions for use without follow in another post, but there may be dragons down that road.

hochmania’s picture

#56 worked for me, despite 1 hunk of the patch failing. Thank you @Vector!

Vector-’s picture

I'm guessing Hunk #5 failed? That's the dependency on #1986532: Support organic groups OG reference widget.

Shouldn't break anything, but that hunk failing leaves behind a couple lines that might cause warnings and clutters up the field's form data a bit. The dependency is there because I'm not sure about validation working properly in all cases without it, but haven't dug any deeper than that.

I wasn't going to bother to reroll this clean on dev until I had some time to inspect the tests further, but here's the bit that failed to change, should be around line 384 if that patch is all you applied:

      $element_matching[LANGUAGE_NONE]['#ajax'] = array(
        'callback' => 'reference_option_limit_js',
        'wrapper' => $settings['ajax_wrapper'],
        'method' => 'replace',
        'effect' => 'fade',
        'event' => 'change',
      );

Should look like this:

      $element_matching[LANGUAGE_NONE]['#ajax'] = array(
        'callback' => 'reference_option_limit_js',
        'effect' => 'fade',
        'event' => 'change',
      );

Or like this if you need the wrapper in the #ajax for some reason (like hacking tests to work):

      $element_matching[LANGUAGE_NONE]['#ajax'] = array(
        'callback' => 'reference_option_limit_js',
        'wrapper' => $ajax_wrapper,
        'method' => 'replace',
        'effect' => 'fade',
        'event' => 'change',
      );
hochmania’s picture

@Vector-

Thanks, replacing the code with this snippet removed an error and it appears to all work correctly. I owe you a beer at Drupalcon

$element_matching[LANGUAGE_NONE]['#ajax'] = array(
        'callback' => 'reference_option_limit_js',
        'effect' => 'fade',
        'event' => 'change',
      );