Hi!

I'm still a newbie in Drupal. Recently I started using form API and I've encoutered a little problem. In my form I need to know who is filling it. I tried to use global $user but inside form it is invisible. Tried to surf the net for solution but came up with nothing (or I just missed it not knowing I have found it :P ) Thx for any advice!

Comments

sepla’s picture

The global $user variable should work, take a look at this example, It's tested and it's working:


/**
 * Test form.
 *
 * @ingroup forms
 */
function test_example_form() {
  global $user;
  $form = array();

  $form['test_user_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Current username'),
    '#default_value' => ($user->uid) ? $user->name : t('You are not logged in.'),
  );
  
  // Test dump. 
  // use dpm() instead if you have devel installed.
  drupal_set_message(print_r($user));

  return $form;
  );
}
SmokOO’s picture

Tried similar thing before but #type was 'hidden' ... and that field is still empty even if I changed to 'textfield'. #value or #default_value also makes no diffirence. I checked query statement many times and even rewrote it. All data except 'user' goes to data base.

sagar ramgade’s picture

Hi,

you need to first declare global $user, which will give you a user object, in order to get user id use $user->uid .

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form

SmokOO’s picture

I've done that before :) and outside $form this variable is visible... inside not :/

SmokOO’s picture

So the code is like that (short version):

$global user;

  function dodaj_sprzet_menu() {
    //some items here
    );
    return $items;
  }
  function dodaj_sprzet_form() {
    return drupal_get_form('sprzet_form');
  }
  function sprzet_form($form_state) {
  //other form fields

    $form['user'] = array(
    /*  '#type' => 'hidden',
      '#default_value' => $user->uid,*/
      '#type' => 'textfield',
      '#title' => t('Current username'),
      '#default_value' => ($user->uid) ? $user->name : t('You are not logged in.'),  // On every user it shows 'You are not logged in.'
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => 'Zapisz',
    );
    // Adds a new button to clear the form. The #validate property
    // directs the form to use a new validation handler function in place
    // of the default.
    $form['clear'] = array(
      '#type' => 'submit',
      '#value' => 'Zresetuj',
      '#validate' => array('sprzet_form_clear'),
    );
    return $form;
  }
  function sprzet_form_clear($form, &$form_state) {
      $form_state['rebuild'] = TRUE;
  }
  function sprzet_form_validate($form, &$form_state) {
     //custom validation
  }
  function sprzet_form_submit($form, &$form_state) {
   //here i try to use $form_state['values']['user']
  }
sagar ramgade’s picture

If you want to use $user inside sprzet_form($form_state), you need to write global $user inside the function ie. function sprzet_form($form_state){
global $user;
...
}

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form

sepla’s picture

Sagar is right, in the #2 code example you see the global $user; statement inside the form callback.

SmokOO’s picture

And now it works :D Big THX everyone!