0

I have a content type called Point-of-Contacts, basic info for a person. (~600 nodes)

I have another content type, like a form, that has a select list of all Points-of-Contacts. When a user selects their desired Point-of-Contact, by name from the select list, I need the rest of the info of that Point-of-Contact to auto-fill text boxes on the form.

Any ideas?

NOTE: Using Drupal 7

  • ajax could consider an option – Aboodred1 Apr 18 '13 at 20:01
  • are you using custom form for Points-of-Contacts or the node content type form?! – Aboodred1 Apr 19 '13 at 15:40
  • I created a new content type for Points-of-Contact. Though I may go back and import into as Drupal users in the People section. Regardless, I still need the same functionality. – Modifier Apr 20 '13 at 16:45
1

In my example I've created two content types with two custom fields in addition to your node reference field:

  1. Node type: Point-of-Contact (machine name points_of_contacts)

    • Text field: To field one (machine name field_to_field_one)
    • Text field: To field two (machine name field_to_field_two)
  2. Node type: Example (machine name example)

    • Node reference: Point-of-Contact (machine name field_points_of_contacts)
    • Text field: Field one (machine name field_field_one)
    • Text field: Field two (machine name field_field_one)

Then You need to create a custom module to alter node reference dropdown field to ajax dropdown with callback function.

First create custom module by following this tutorial

Second alter node reference field by implement this hook hook_field_widget_form_alter

function MODULE_NAME_field_widget_form_alter(&$element, &$form_state, $context) {

  if(isset($element['#field_name']) && $element['#field_name'] == 'field_points_of_contacts') {
    $element['#ajax'] = array(
      'callback' => 'MODULE_NAME_auto_fill_callback',
    );
  }
}

Third implement ajax callback function

function MODULE_NAME_auto_fill_callback(&$form, &$form_state) {

  $commands = array();

  if(isset($form_state['values']['field_points_of_contacts'])) {
    // get selected item/node from dropdown list
    $nid = $form_state['values']['field_points_of_contacts']['und'][0]['nid'];

    // load node by nid
    $node = node_load($nid);

    if($node) {

      if(isset($node->field_to_field_one['und'])) {
        // fill text field 
        // edit-field-field-one-und-0-value represent the generated html id of the text field 
        $commands[] = ajax_command_invoke('#edit-field-field-one-und-0-value', 'val', array($node->field_to_field_one['und'][0]['value']));  
      }        

      if(isset($node->field_to_field_two['und'])) {
        // fill text field 
        // edit-field-field-two-und-0-value represent the generated html id of the text field 
        $commands[] = ajax_command_invoke('#edit-field-field-two-und-0-value', 'val', array($node->field_to_field_two['und'][0]['value']));  
      }
    }
  }

  return array('#type' => 'ajax', '#commands' => $commands);
}

Note: change MODULE_NAME to your desirable module name.

| improve this answer | |
  • 1
    Wow, I'm going to try this out now...thx! I will post feedback soon. – Modifier Apr 23 '13 at 18:21
0

It must be late now but there is a module called Entity Reference Autofill that can solve this issue.

| improve this answer | |

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.