Dear friends,

I'm using this module in few projects and working like a charm. But now I have this situation:

Content Type A - Countries
Content Type B - Cities (with reference of Countries)
Content Type B - Regions (a text field)
Content Type C - Address

In the content type C (address) I select the country and I can see just the cities of that country. But I need more: select a country that's show me the cities, select a city that's show me a region.

How can I do that? Is this possible using this module or I need to find a different approach?

Just to remember, the region field IS NOT a entity reference field, just a text field.

Thank you!

Comments

joachim’s picture

> How can I do that? Is this possible using this module or I need to find a different approach?

I don't know. Try it. It may be it's beyond what the Ajax for this module can do. It would be possible with more advanced JS, but that's out of my area of expertise.

joachim’s picture

Closed #1835466: Please confirm that the module won't allow more one limited field in a heirarchy as a duplicate.

It may be that the problem is to do with re-attaching JS behaviours, but it would need someone with better JS skills than mine.

jonhattan’s picture

Issue summary: View changes

I'm not sure re-attaching will fix this but IMO it won't be a satisfactory solution: an ajax reload for each element in the chain is not the best UX.

I think there's a chance to rebuild all elements implied and return the proper ajax commands.

jonhattan’s picture

"Related issues" is not working atm. So related: #1986526: Support one field changing 2+ fields

jonhattan’s picture

Component: Documentation » Code
Category: Support request » Feature request
jonhattan’s picture

Status: Active » Needs review
StatusFileSize
new3.11 KB

Here's a first approach. It's working nice for my use case (4 fields in chain), but thw whole code involved here deserves a refactoring. I'll wait for feedback before trying to refactor it.

Note: this patch depends on the one at #1986526-17: Support one field changing 2+ fields

Status: Needs review » Needs work

The last submitted patch, 6: reference_option_limit-1809338-6.patch, failed testing.

jonhattan’s picture

Above solution doesn't work when field instances are created in an order different than the order you want in the chain (usually the same order as the weights set for instances in the bundle).

I found this problem when exporting fields with features. They're recreated in alphabetically order and the order by database id is lost.

This may be fixed by a process in two steps in reference_option_limit_form_alter().

joachim’s picture

How does the creation order and numeric field ID affect things in reference_option_limit_form_alter()?

jonhattan’s picture

No, it affects the order of the calls to reference_option_limit_field_widget_form_alter(). In the seval calls to this function, $form_state['reference_option_limit'] is populated, and used afterwards in reference_option_limit_form_alter().

joachim’s picture

So is that because hook_field_widget_form_alter() is called in field ID order, rather than field weight within the entity bundle?

jonhattan’s picture

Sort of. It seems there's no declared order in the code. Instances are retrieved by

_field_invoke_get_instances() -> field_info_instances() -> FieldInfo::getInstances() -> ...

getInstances() method calls field_read_fields() and field_read_instances(), that don't declare any order for the query.

joachim’s picture

The query can't do that, because the weight of the field instance is inside the serialized data. So it would have to be up to field_info_instances() to do some ordering.

It sounds to me like this could be considered a core bug, as the order the hook is invoked in will be arbitrary.

jonhattan’s picture

bug or not, we should work around it. For some strange reason one may want to limit a field's options by a matching field with a higher weight.

jonhattan’s picture

StatusFileSize
new2.65 KB

Added a fix for the case when there're no match values.

mas0h’s picture

Patch #15 refuses to apply, Also does it support three fields in chain with views?