I have a user activation/registration form with different attribute like

Captcha and security questions which are coming from different modules.

I want to change the display order of form elements.

and i am using this code in my custom module

function security_question_form_user_register_form_alter(&$form, &$form_state) {
  if ($form_id == "create_account_form") {
    $form['#pre_render'][] = 'security_question_form_alter_weight';

function security_question_form_alter_weight($elements) {
  $elements['questions']['#weight']= -47; 
  return $elements; 

It's not working. Can any one have any solution?

  • A quick thought here, I think $element is passed by reference so you should add & before it and no need for the return statement – Ismail Cherri Jul 4 at 9:38
  • 2
    @IsmailCherri No, as the documentation says The function(s) provided in #pre_render receive the element as an argument and must return the altered element. – kiamlaluno Jul 4 at 11:26

You can do this directly inside hook_form_alter, like this:

function security_question_form_user_register_form_alter(&$form, &$form_state) {
  $form['questions']['#weight'] = -47;

Then there is no need to create a custom function to do it.

Another thing, you can't implement hook_form_FORM_ID_alter and then use $form_id inside that. You must implement either hook_form_alter, and check if the form id is the one you want, or implement hook_form_FORM_ID_alter and write the form id in the function name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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