1

I'm trying to create content with REST, everything worked correctly until we need to an entity Reference field,I add it to my content type and in want to pass entity referenced nid to it with

Part of my code :

    formValues.gender = parseInt($('input[name=optradio]:checked').val());
    var postData = {};
    postData.title = [{'value': formValues.name }];
    postData.field_enroll_email = [{'value': formValues.email}];
    postData.field_gender = [{'value': formValues.gender }];
    postData.field_phone_number = [{'value': formValues.phone }];
    postData.field_course_ref = [{'value': formValues.nid }];
    if(formValues.ac != "") {
        postData.field_academic_certificate = [{'value': formValues.ac }];
    }
    if(formValues.ec != "") {
        postData.field_english_certificate = [{'value': formValues.ec }];
    }
    postData.type = 'enroll';
    jQuery.ajax({
        url: "/entity/node",
        method: "POST",

my entity referenced machine name field is field_course_ref and I tried following method (formValues.nid contain nid of Node that I want to referenced it )

    postData.field_course_ref = [{'value': formValues.nid }];

How can I fill entity referece field with REST ?

  • The entity reference needs to contain the UUID, not the ID. Just GET a node of that type first and then use the same structure. But those validation errors are about the title and the field_enroll_email fields and apparently has nothing to do with the entity reference. Are you sure it works without the reference? – Berdir Jul 17 '16 at 13:13
  • @Berdir oops, I should update my question, I update the question, can you tell me after I got UUID of node, how can I pass it , is this ok? postData.field_course_ref = [{'value': uuid }];` (let's suppose I put uuid in uuid variable) – Yusef Jul 17 '16 at 13:25
2

After some research (How to POST an "entity reference" field to a node ? , REST views entity reference issue

I found the solution, we should pass nid as target_id, final asnwer is

postData.field_course_ref = [{'target_id': formValues.nid }];

it's work correctly, I answer my question maybe be helpful for another guy later.

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.