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?
2 Answers
Please make a try by using Entity reference autofill and Entityreference prepopulate Modules.
- Add an entity reference field.
- The entity reference autofill will automatically fill reference value into field.
- 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?– neethuCommented Nov 16, 2015 at 9:49
-
-
-
-
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– neethuCommented Nov 16, 2015 at 10:18
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 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!
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?– neethuCommented Nov 16, 2015 at 9:51