AnswerBun.com

How to remove default form tag while rendering output in hook_theme

Drupal Answers Asked on January 2, 2022

I am customizing the login form. I have to put all the three forms(login, register, forgotpassword) into one page.
I don’t want that unwanted root form tag, how can i remove that one? I am using drupal 7. This is my code.

template.php

function bartik_theme() {

$items = array();

$items['user_login'] = array(
    'render element' => 'div',
    'arguments' => array('form' => NULL),
    'variables' => array('form' => NULL),
    'path' => drupal_get_path('theme', 'bartik') . '/templates',
    'template' => 'user-login',
    'preprocess functions' => array(
          'bartik_preprocess_user_login'
    ),

);

return $items;

}

user-login.tpl.php

        <div id="signupbox">
            <?php
                $login_form = drupal_get_form ( 'user_login_block' );
                echo drupal_render ( $login_form );

                $forgot_password_form = drupal_get_form ( 'user_pass' );
                echo drupal_render ( $forgot_password_form );

                $register_form = drupal_get_form ( 'user_register_form' );
                echo drupal_render ( $register_form );
            ?>
        </div>

My problem is that all the 3 forms are rendering in one more form tag.

My Output:

<form action=”/drupal728/user” method=”post” id=”user-login”>

 <form>Login</form>
 <form>Password....</form>
 <form>Registration....</form>

</form>

I don’t want that unwanted root form tag, how can i remove that one? I am using drupal 7.

One Answer

You're attacking the problem at the wrong level - if you want to modify forms/functionality, the theme layer's too late. Specifically what's happening here is that by default forms have a theme wrapper of theme_form() which adds the form tag. The form template itself should only contain the form's children elements. (And shouldn't contain for example calls to drupal_get_function()).

Here are some ways of adding the forms:

  1. Implement hook_menu_alter() and alter the page callback for the login page to be your own callback. This callback returns the three forms. Don't forget to check for whether the user's logged in like core does.
  2. Create a couple of custom blocks for the other two forms and add them to the login page.
  3. Implement hook_page_build() and add the forms there (quick and dirty!).

Answered by Andy on January 2, 2022

Add your own answers!

Related Questions

How to create and check block revisions?

1  Asked on November 8, 2021 by user3560198

   

Custom module with config exposed in json api

1  Asked on November 8, 2021 by daniel-harper

   

Not able to see < character in front end

2  Asked on November 5, 2021 by jos-l

 

Limit filter operators

1  Asked on November 5, 2021

   

Dynamic filters based on another filter

1  Asked on November 5, 2021 by soundcheese

   

How to add a field to menus?

1  Asked on October 26, 2021

     

Drush cr is responding with Bootstrap Failed

0  Asked on October 26, 2021 by chanel

 

How do I create a table when a module is installed?

1  Asked on October 26, 2021 by david-montera

     

Approaches for multi-field values

0  Asked on October 26, 2021 by ian-stewart

   

Pager not being rebuilt after $form_state->setRebuild() is called

0  Asked on October 26, 2021 by kenny83

 

Where are Commerce 2 hooks (or Events) in Commerce 2

1  Asked on October 26, 2021 by wally-kolcz

     

Show own content in Media Library

0  Asked on October 26, 2021 by rafaticarte

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP