Hi,
I hope someone can shed some light on what I initially thought would be straight forward :)

Briefly, I want to allow users to add employees through an employee content type I've created.

If the user role is Admin, then they would be able to select all sites from a select list to assign an employee to any site.

If the logged in user works at siteA then any employee they add would have the employee site field automatically populated with siteA an dthis filed would be hidden. The users site is identified by their role.

I'm ok hiding the field but my questio nrelates to the two functions :-

Function form_after_build & form_alter

I can pass the $form_id into the form_alter function, but apparently can't set the #value of the site field using this function

form_after_build does accept the $form_id but does seem to offer the functionality to set the #value of a cck select list.

Am I barking up the wrong tree concentrating on these two functions?

Comments

scrypter’s picture

should be all you need. You can change virtually anything about a form using this hook. Does you function look like this:

function mymodule_form_alter(&$form, $form_state, $form_id) {
//  print '<pre>' . htmlspecialchars(print_r($form, TRUE)) . '</pre>'; die;
  if ($form_id == 'blahblah') {
    $form['submit']['#value'] = 'Go';
  }
}

You need the & in front of $form.

www.purpleoar.co.nz/scryptik - Javascript editor with syntax error checking
www.purpleoar.co.nz - Web development, Drupal consultancy

captainhirez’s picture

thanks for your reply scrypter, much appreciated :)

That's pretty much what I've been playing around with, I'll try it out again when I'm back in the office tomorrow.

I can't remember the error thrown ( I'll try and recreate and post it here if that helps) but it seems Drupal isn't happy using that function to change the value of a select list. I found this article on it :-

http://drupal.org/node/726282

Once again thanks for your help.

scrypter’s picture

The options for a select list are set in '#options'. Its an Array. If you uncomment the print_r debug you will see the existing $form structure.

www.purpleoar.co.nz/scryptik - Javascript editor with syntax error checking
www.purpleoar.co.nz - Web development, Drupal consultancy

pwhite’s picture

This will change a select box to a hidden field.

function example_form_alter(&$form, $form_state, $form_id) {
      switch ($form_id) {
      case 'example_node_form':
        $form['#after_build'][] = 'example_after_build';
        break;
    }
  }
	
function example_after_build($form, &$form_state) {
	$form['field_example']['#value'] = 'example';
	$form['field_example']['#type'] = 'hidden';
	$form['field_example']['#name'] = 'field_example[value]';
	return $form;
}
captainhirez’s picture

Thanks you two, problem solved :)