2

I think theme_form() is responsible for rendering forms! I want to change the look of a form, a lot. I think just instead of theme_form I can use my custom theme function/template file.

I can use hook_form_alter to change any form's structure using my module. Now my question is, how do tell the form to use a theme function/template I've created?

Is there a #theme property (or something like that) available for forms, which I can set to my theme function/template file?

P.S. I checked out this but I need more explanation, when the case is: I'm trying to theme a form defined by another module/core modules.

2

Open template.php of your theme

function YOURTHEMENAME_theme($existing, $type, $theme, $path) {
  $hooks['form_id_of_form'] = array(
    'template' => 'path_to_tpl_file',
    'arguments' => array('form' => NULL),
  );
  return $hooks;
}

Also you can try

function hook_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'form_id_of_form') {
    $form['#theme'] = 'path_to_tpl_file';
  }
}
  • This is not quite right. In your hook_theme, you declared the theme callback to be "form_id_of_form". Therefore, the hook_form_alter should implement $form['#theme'] = 'form_id_of_form', not 'path_to_tpl_file' – aaronbauman Feb 11 '15 at 19:06
2

Is there a #theme property (or something like that) available for forms, which I can set to my theme function/template file?

Yes there is. Take a look at the #theme key in the Form API Reference. Take a look at the code for theme_form() for the default implementation.

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.