0

I have content type "A" and I have field_users, and field_mail, where in the fields_users is entity reference field referring node of users, now when we select the user from the select list automatically the field_email should update based on the user selected since the user already created in D7.Is there any way to achieve this?

1

Please make a try by using Entity reference autofill and Entityreference prepopulate Modules.

  1. Add an entity reference field.
  2. The entity reference autofill will automatically fill reference value into field.
  3. Entityreference prepopulate will prepopulate the values. (you have to add existing fields for that.)

For more details please refer project page.

Another approach using ajax. Try the following code in a custom module.

function your_module_form_alter(&$form, &$form_state, $form_id){ 
  if($form_id =='your_node_form_id') {
     $form['your-email-field']['#prefix'] = '<div id="field-email-replace">';
     $form['your-email-field']['#suffix'] = '</div>';
}
$form['your-user-field']['und'][0]['#ajax'] = array('callback' => 'your_module_callback', 'wrapper' => 'field-email-replace','method' => 'replace',); 
}

function your_module_callback($form, $form_state){
  $uid = $form_state['values']['your-user-field']['und'][0]['value'];
  $user = user_load($uid);
  $form['your-email-field']['und'][0]['value'] = $user->email;

  $commands = array();
  $commands[] = ajax_command_replace("#field-email-replace",  render($form['your-email-field'])); // use render if you want to render field.   

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

Hope this will help you.

  • how can i add the existing email field which is there in add user page[by default] in content type? – neethu Nov 16 '15 at 9:49
  • check drupal.org/node/2134073. – Abin Nov 16 '15 at 10:05
  • do i need to create rule for it? – neethu Nov 16 '15 at 10:10
  • check with the video tutorial – Abin Nov 16 '15 at 10:15
  • youtube.com/watch?v=1I09Dnhm4lQ I have seen this....and here need to add the existing field! here i need to add user email field from user registration form....but that field is not showing up while adding existing field – neethu Nov 16 '15 at 10:18
0

If you want it to happen live, and all fields are on the form already, safest way would be to use hook_form_FORM_ID_alter() to add following to a form:

$form['#attached']['js'] = array(
    drupal_get_path('module', 'module_name') . '/js/copy_field_value.js',

Then in copy_field_value.js create behavior:

(function($) {
  Drupal.behaviors.moduleNameCopyFieldValue = {
    attach: function (context, settings) {

      // Repeat this for all fields as needed
      $('#source', context).on('blur', function(){ 
        // above you can use change instead of blur if element is not changed by another js
        if(!$('#destination').val() || 0 === $('#destination').val().length) {
          $('#destination').val($(this).val());
          // wrap line above in "if no value" like I did, or other condition you like
        }
      });
      // end of "repeat this"

    }
  };
})(jQuery);

Reference click here

If you are not good in programming

Use one of these modules:

Computed Field

Computed Field is a very powerful field module that lets you add a custom "computed fields" to your content types. These computed fields are populated with values that you define via PHP code. You may draw on anything available to Drupal, including other fields, the current user, database tables, you name it. (Feeling the power yet? :) ) You can also choose whether to store your computed field values in the database with other content fields, or have them "calculated" on the fly during node views. (Although you should note that Views use requires database stored values.) This field is literally the Swiss Army knife of fields. So start cooking up your PHP based values!

Entity reference autofill

The Entity reference autofill module gives Entity reference fields an option to populate other form fields with data from selected referenced entities.

  • i tried entity reference autofill i have selected entity reference field as users, and i want to add email field existing one [by default] from add user page how can i? its not available? – neethu Nov 16 '15 at 9:51

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.