4

I have a form in which field my_consultant_id is "Hidden integer" type field and having widget type as "Hidden field".

I am assigning the value to this field by using hook_form_alter

function threebl_summary_report_form_alter(&$form, &$form_state, $form_id) {
    if($form_id == 'eck__entity__form_add_summary_report_comparative_client_gro‌​up') {
        global $user; 
        $form['field_created_by']['und'][0]['#default_value'] = (int) $user->uid;
    }
}

But not able to set the value for that field.

If I inspect the form it shows html code for field like:

<div class="field-type-field-hidden-integer field-name-field-my-consultant-id field-widget-field-hidden form-wrapper" id="edit-field-my-consultant-id">

Thanks in advance.

  • Do you get any errors or it does not store the value to the database? The form submit handler is custom (share the code) or default? – mchar Feb 7 '17 at 10:28
  • No errors...It does not store the value in database. I checked the value of $user->uid its correct – Santosh Jagtap Feb 7 '17 at 10:34
  • Check this out: drupal.org/node/728398#comment-11519357 – mchar Feb 7 '17 at 10:52
3

I managed to do it by validate handler of the form. Using validate handler I assigned my value to the form variable before submit.

In form alter hook call validate handler like below

$form['#validate'][] = 'my_module_name_form_validate';

And following is the validate handler function code

function my_module_name_form_validate(&$form, &$form_state) {    
$form['field_my_field']['#parents'] = array('field_my_field');
form_set_value($form['field_my_field'],array('und' => array(0 => array('value' => $new_value))),$form_state);

}

For reference please go through link

| improve this answer | |
0

I think you are missing default value.

try

$form["my_consultant_id"][LANGUAGE_NONE][0]['value']['#default_value'] = (int)$user->uid;
| 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.