8

I want to theme the user registration form in Drupal 7 by adding new markup, etc. I cannot seem to figure out how to do it.

I've followed a few tutorials which suggested that you could simply invoke hook_theme in template.php, and use a corresponding theme function:

function mytheme_theme(){
  return array(
    'user_register' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

function mytheme_user_register($form) {
  // print, die, etc
}

But this is not doing anything, regardless of what I do in the theme_user_register() function, or how many times I clear the caches.

Any thoughts about how to do this in D7?

5 Answers 5

9

It turns out that I need a combination of changes:

I had to implement hook_form_alter()in a module and set the form's #theme property:

function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['#theme'] = 'user_register';
}

And then in template.php:

function mytheme_theme($existing, $type, $theme, $path){
  return array(
    'user_register' => array(
      'render element' => 'form',
      'template' => 'templates/user-register',
    ),
  );
}

function mytheme_preprocess_user_register(&$variables) {
  $variables['rendered'] = drupal_render_children($variables['form']);
}

And then I added user-register.tpl.php with my desired markup, and

<?php echo $rendered; ?> 

to print the form.

1
  • 1
    Remember to accept one of the answers (the best, can be yours) so that this question is correctly marked as resolved.
    – Berdir
    Commented Jun 2, 2011 at 20:16
3

The definition of the theme function given from the implementation of hook_theme() is wrong:

  • In Drupal 7, the array index "arguments" is not used anymore; you should use "render element" (as in this case), or "variables."
  • The theme function receive a different argument, in Drupal 7, which is &$variables; in your case, you should use $form = $variables['form'], once you defined the theme function using the array index I reported in the previous point. See what done in theme_locale_date_format_form() (the theme function used for the form defined by locale_date_format_form(), which is defined in locale_theme(), or in theme_status_report(), which is defined in system_theme().
0
2

You can use hook_form_alter to alter the form elements and add custom markup by adding markup form elements. It's also possible to set which theme function to use which could be your own custom theme function.

See the FAPI for more details.

0
2

I think this is possible without having to do any modules. There is a built-in theme suggestion for the user registration page and it is page--user--register.tpl.php

1

Here is the exact function:

@see hook_form_FORM_ID_alter

function hook_form_user_login_alter(&$form, &$form_state, $form_id) {
  // Modification for the form with the given BASE_FORM_ID goes here. For
  // example, if BASE_FORM_ID is "node_form", this code would run on every
  // node form, regardless of node type.

  // Add a checkbox to the node form about agreeing to terms of use.
  $form['terms_of_use'] = array(
    '#type' => 'checkbox', 
    '#title' => t("I agree with the website's terms and conditions."), 
    '#required' => TRUE,
  );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.