2
  • I have a Course Offerings vocabulary where each taxonomy term is an educational course.

  • Users have a multivalued My Courses Entity Reference field referring to the terms of the courses they're enrolled in.

  • I have a custom content type (Entityform) with two more Entity Reference fields: a Course field and a Student field. The user must first select a course (a Course Offerings taxonomy term), and then select a student (a user).

Here's the kicker: I need to filter the Student select list options based on what the user selected in the Course field.

The Course field select options are generated by a view that returns the current user's My Courses field courses. After the user selects one of their courses, the Student field select options should be generated by a view that uses the course they chose as a contextual filter to return a list of only the users enrolled in that course.

I know how to build the view once I have the contextual filters, but I'm stumped as to how to pass the Course term to the view as a contextual filter. Is this possible?

EDIT FOR CLARIFICATION:

  • Both the Course and Student fields are Entity Reference fields.

  • Both use select-list widgets whose options are populated with views Entity Reference displays.

  • The Course field comes before the Student field on the eform. The intention is that the person filling out the form will first choose the course. Then they'll choose the student from a list of students enrolled in the course they just selected.

  • I don't quite get what you are trying to do, but did you try making a Entity Reference display for the view, and then using that for the Entity Selection source on the form? – mpdonadio Apr 1 '13 at 15:53
  • Yep, I've got that bit working. The trick is I need to filter that view (Entity Reference display) to only show students enrolled in the previously selected course. I'll add a bit more description to the question to hopefully clarify what I'm trying to do. – joe_flash Apr 1 '13 at 15:56
  • looking for same feature and still not get answer :(. – kiranking Jul 13 '13 at 20:37
1

I've kept an eye out for a module that does this since the original post, but never came up with anything. I've been using a sort of "one off" solution to do this on a case-by-case basis instead:

  1. In a custom module, implement hook_form_FORM_ID_alter() for the form with your dynamic fields.

  2. Add #ajax attributes to the first element to trigger and handle changes to it with AJAX. This will specify the AJAX callback handler and the HTML ID of the element to be replaced with the AJAX response.

  3. In the ajax callback for that element, use the AJAX-submitted value to dynamically get the appropriate values for the second value. For me, this is usually a query of some sort with the submitted value as a filter.

  4. Update the second field's form element #options attribute with the dynamic values and return the element.

  • I should also mention that there is limited support for this built into Entity reference. A decent tutorial for how to use it can be found here. – joe_flash Sep 2 '14 at 22:44
  • 1
    The tutorial seems to have gone away, but it used the solution @muvasco mentions. – joe_flash Mar 9 '15 at 22:00
1

Take a look at the Reference field option limit module, it also does what you want.

This module allows reference fields of several types to have their widgets' available options limited by the values of other fields in the current entity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.