Hi guys trying to theme the registration form surprisingly there is very little information out there on how there can be done except if you want to create a module to do this

I was trying to use

function phptemplate_user_edit_form($op,$form) {

$form['account']['name']['#title'] = 'Mr Whippy';
return drupal_render($form);
}

In the template.php file but not working

Is my only option to create a module to do this – if so what are the key hooks that need to be added

Comments

AjK’s picture

hook_form_alter()

snippet:


function mymodulename_form_alter ($form_id, &$form_values) {

  if ('user_register' == $form_id) {
    $form_values['account']['name']['#title'] = 'Mr Whippy';
  }
}

Notice the &$form_values as a parameter which means you get the form values by reference thus allowing you to alter them.

somes’s picture

Tried that and for some reason cant get it to work

do you reckon it should work from the template.php file

i'm using

function user_form_alter ($form_id, &$form_values) {

  if ('user_register' == $form_id) {
    $form_values['account']['name']['#title'] = 'Mr Whippy';
  }

}
AjK’s picture

erm, you named your module "user" ???? You need a unqiue module name, "user" is already a core module. You must invent a new one. Look at all the info regarding creating a module.

And no, hook_form_alter() is not availabe in template.php, you need to create a new module to use it.

somes’s picture

heres a module i cobbled together

function test_form_alter ($form_id, &$form_values) {

  if ('user_register' == $form_id) {
  print_r($form_values);
  
  	$newform = array();
	$newform['yourvalue'] = array(
	'#type' => 'textifled',
	'#title' => t('Your Value'),
	'#default_value' => variable_get('yourvalue', 0), );
	
	
	$form_values = array_merge($newform, $form_values);
  }

}

but cant add the new field onto the end of the form - any pointers

AjK’s picture

$form_values = array_merge($newform, $form_values);

The above code in your function is not needed. Note, in your function declaration, you declare &$form_values. That leading & means "pass by reference" so you can mod the array without having to merge.

So, it should look like this:-

function test_form_alter ($form_id, &$form_values) {

  if ('user_register' == $form_id) {
    $form_values['yourvalue'] = array(
      '#type' => 'textifled',
      '#title' => t('Your Value'),
      '#default_value' => variable_get('yourvalue', 0), 
    );
  }
}

One point to note. You have added an extra textfield to the form. But how can you intercept it and use it? It's "lost" otherwise. You need a submit handler to capture the field value and do something with it. So, lets expand your snippet:-

function test_form_alter ($form_id, &$form_values) {

  if ('user_register' == $form_id) {
    $form_values['yourvalue'] = array(
      '#type' => 'textifled',
      '#title' => t('Your Value'),
      '#default_value' => variable_get('yourvalue', 0), 
    );
    $mysubmit = array('test_mysubmit_handler' => array());
    $form_values['submit'] = isset($form_values['submit']) ? array_merge($form_values['submit'], $mysubit) : $mysubmit;
  }
}

function test_mysubmit_handler($form_id, $form_values) {
  // handle your field in here, it'll be in $form_values['yourvalue']
}

Note what's going on here. You used _form_alter() to add a new field. But Drupal has no idea what that new field is at submit time. You need to tell Drupal what to do about it. So, you "chain" your own handler function in. The ternary is used to detect if ['submit'] already exists, if it does, it merges in your new handler. If it doesn't, it get's defined. Drupal will merge in it's own handler at the end of the form build process.

It's also worth noting that if you modify a _node_form (ie a node edit form of some type) then you do not need a "mysubmit_handler" at all. You should instead use hook_nodeapi(). Why, well, you can chain in a handler if you want, but your function will be called before nid is defined for new node submissions. So it's rather hard for you to relate you new field to an nid. In that case, use hook_nodeapi() $op == 'insert' and you values will be in the node array and the nid will be defined, allowing you to link your custom data with the node.

That any clearer?

somes’s picture

Hi Andy

Clear as mud at the moment but its a whole lot better than what I had been doing :)
only got around to having a look at it last night

tks for taking the time out to sort that out

there seems to be a problem with

$mysubmit = array('test_mysubmit_handler' => array());
$form_values['submit'] = isset($form_values['submit']) ? array_merge($form_values['submit'], $mysubit) : $mysubmit;

has this piece of code got any thing to do with having to create a submit handler as here (or the problems with)

http://drupal.org/node/150817

tks again