I'm attempting to use a "registration code" entered during a user's registration to create an entity reference to a node with a corresponding code.

Any help is greatly appreciated.

Specific use case:

"Professor" (users) have "Classes" (nodes) that contain a text-field with a unique 5-digit code (created with Computed Field).

"Students" have a "Registration Code" field on their registration & user profile page (aka a text field on the user entity) for manually entering a 5-digit code that should "assign" them to a "Class" that has the same registration code.

I'm not a programmer, but this might make more sense:

if [Student user's 5-digit registration code] = ["Class" node's 5-digit registration code] then an entity reference is created between [student user] and ["class" node]

I'd like the rule to trigger when a user updates the registration code field.

Essentially, I'm attempting to avoid displaying Entity Reference's autocomplete widget to users.

1 Answer 1


I don't think I would start with Rules, especially from a performance standpoint. I would first try something like this:

  • in the Student content type, create an Entity Reference field for the reference, making sure it has a default value, say 0 for ease of access later
  • Using Field Permissions, hide that field to everyone but admin

The Field Permissions module allows site administrators to set field-level permissions to edit, view and create fields on any entity.

  • create a custom module

  • using hook_node_validate(), check the list of Classes for the existence of the correct code

  • using hook_node_presave(), update the hidden reference field once it is validated

Now you have something that allows the Student to enter a value, have it validated, and then copy that value into a proper reference field for later use


I was curious if I was correct in my initial answer, so I figured it out to check, and it worked - for this example:

  • Create a content type Class, using the title field as the Class Code

  • Create a content type Student

  • Add field to Student content type: field_student_class_code, entity reference to Class content type

  • use Field Permissions to hide field_student_class_code to everyone but Admin

  • Add field to Student content type: field_student_visible_class_code, text field

  • Create a new module called custom_node_reference, and include this code:

    function custom_node_reference_node_validate($node, $form, &$form_state)
      if($node->type == 'student')
        $lang = $form['field_student_visible_class_code']['#language'];
        $class_value = check_plain($form['field_student_visible_class_code'][$lang][0]['value']['#value']);
        $result = db_select('node', 'table_alias')
            ->condition('type', 'class', '=')
            ->condition('title', $class_value, '=')
        if($result->rowCount() <= 0) 
          form_set_error('field_student_visible_class_code', 'That class code does not exist, please re-enter a valid class code');
    function custom_node_reference_node_presave($node)
      if($node->type == 'student')
        $lang = $node->language;
        // get entity_id for current class code
        $val = db_select('node', 'n')
          ->fields('n', array('nid'))
          ->condition('type', 'class', '=')
          ->condition('title', $node->field_student_visible_class_code[$lang][0]['value'], '=')
          $node->field_student_class_code[$lang][0]['target_id'] = $val;

If you change the machine names of the fields, you will need to adjust the code to suit

  • Thanks for the quick reply, Geoff! I don't know php and have yet to delve into even simple custom modules and hooks, but this looks like it might be a good project for me to try to tackle while learning. Jun 18, 2015 at 19:31
  • @user2461917 see my edit...
    – Geoff
    Jun 18, 2015 at 21:58
  • Wow! Thank you! I never would have figured that out. I hope this answer helps someone else as well. Jun 18, 2015 at 22:36

Your Answer

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

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