Hello,

I'm hoping somebody could help me with this. I want to pre-populate and hide a userreference if a parameter is passed in the through a url. E.g.

node/add/lesson?person=5

My solution so far is to set the default value and hide the field using CSS.

<?php
function MYMODULE_form_alter(&$form, &$form_state, $form_id){
  if(is_numeric($_GET['person'])){
        $person = user_load(array('uid'=>$_GET['person']));
        if($teacher->uid){
          $form['field_person']['#prefix'] = '<div class="form-item"><label>Person:</label> '.$person->name.'</div><div style="display:none">';
          $form['field_person']['#suffix'] = '</div>';
          $form['field_person'][0]['#value'] =$person->uid;
          $form['field_person'][0]['#default_value'] =$person->uid;
          $form['field_person']['#default_value'][0]['uid'] =$person->uid;
        }
      }
}
?>

This works fine (so maybe this will help some people), but is there any way I can set the type of the field to 'hidden'? When I tried it using

<?php
$form['field_person'][0]['#type'] ='hidden';
?>

it didn't do anything. Although I should add that I'm using a view to also add the possible options.

Any pointers would be much appreciated.

Comments

russellb’s picture

I've been looking at pre-populating and hiding fields too. It looks to me like prepopulate doesn't work with hidden fields.

xanderol’s picture

Unfortunately when you hide the field it prevents the Pre-populate module from setting the value of the field.

If you are trying to make sure the field can't be changed once it is set with prepopulate you can disable the field.

In Drupal 7 you can use

$form['field_fieldname']['und']['#disabled'] = true;

But I'm not sure if that format will work with Drupal 6.

fehin’s picture

Hi xanderol, where does the code go?

xanderol’s picture

You have to create a custom module which calls hook_form_alter like the one in the original post.

fehin’s picture

Thanks. I used $form['field_fieldname']['#disabled'] = true; in my template.php and it worked.