1

I have a content type "Students" which has an entity reference field "Course".

Course field shows a list of course nodes titles from a view.

When adding a new student node, I need the course field to be automatically selected as per the URL value.

I will pass the course ID in the URL like this.

/node/add/student/COURSEID

Any idea how to get this done ? thanks

0

3 Answers 3

1

You could use Entity Reference Prepopulate module

Allow prepopulating any Entity reference field via URL

README.txt

Usage

In order to pre-populate an entity reference field you have to supply the parameter in the URL.

The structure is node/add/article?[field_ref]=[id]

Where [field_ref] is the name of the entity reference field and [id] is the id of the entity being referenced.

Examples:

node/add/article?field_foo=1

node/add/page?field_bar=1,2,3

1
  • 1
    this is something what I was looking for. But i already ended up doing it with jquery.
    – brainHax
    Sep 1, 2016 at 14:46
1

I think the easiest way would be to use hook_form_alter() and set a default value for the field. You can use arg() to get the course ID

0

Another jQuery quick and dirty way.

jQuery( document ).ready(function($) {

    var the_url  = window.location.href;  
    var nid_from_url = the_url.substr(the_url.lastIndexOf('/') + 1);
    //change the selected value of the select list of courses.
    $('.page-node-add-offline-course-pictures #edit-field-offline-course-reference2-und').val(nid_from_url);
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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