Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'm working on a module that has a custom output for the form that's being called, and this form has multiple pages. The multiple page part is being called correctly, but I can't get the theme to render the form.

I've got the following code:

<?php

// Page callback to render the form
function mymodule_main() {
    return drupal_render(drupal_get_form('mymodule_form'));
}

// Picks which form to call
function mymodule_form($form, &$form_state) {
    drupal_set_message('Picking form to use');
    if(!empty($form_state['page_num']) && $form_state['page_num'] == 2) {
        drupal_set_message('Form 2');
        return mymodule_form_two($form, &$form_state);
    } else {
        drupal_set_message('Form 1');
        return mymodule_form_one($form, &$form_state);
    }
}

// Form definition for the first page
function mymodule_form_one($form, &$form_state) {
    return array(
        // ...
        '#theme' => 'mymodule_theme_one',
    );
}

// Hook theme call
function mymodule_theme() {
    drupal_set_message('Hook Theme has been called');
    return array(
        'mymodule_theme_one' => array('render element' => 'form'),
    );
}

// My custom theme for the first page
function theme_mymodule_theme_one($vars) {
    drupal_set_message('Theme for first form has been called');
    // Code to style the form
    return $output;
}

In the theme_mymodule_theme_one function I move some of the form definition things around, unset some variables for output, etc. When I visit my page I defined in my hook_menu() call I get the form I defined in mymodule_form_one but without any of the work done in theme_mymodule_theme_one and I only get the following two messages:

  • Picking form to use
  • Form 1

I don't see any of the other messages.

Edit

I've cleared the cache manually as well as disabled and re-enabled the module and the theme code still is not being called.

share|improve this question
    
Are you saying mymodule_theme() is not invoked just because you don't see the "Hook Theme has been called" message on screen? –  kiamlaluno Mar 23 '12 at 23:03
    
Not entirely. I wasn't sure if I would see it when I cleared the cache/enabled the module, but even if I try and call theme('mymodule_theme_one', $params) I don't get any output. –  dragonmantank Mar 24 '12 at 1:58

1 Answer 1

The only thing you should need to do is clear Drupal's cache; hook implementations are cached so once you've written the function you need to clear the cache to get the new data into the registry.

One other small thing, you probably shouldn't be returning an array like you currently are from your form builder function.

I'm pretty sure Drupal sets up the $form variable it passes into the function through drupal_get_form with some required details, so returning an array (and not the original $form element) will probably mean your return from the function is missing something. The usual way would be to do it like this:

function mymodule_form_one($form, &$form_state) {
  $form['#theme'] = 'mymodule_theme_one';      

  return $form;
}
share|improve this answer
    
mymodule_form_one contains the entire form definition, so should I not initialize $form to an array and then define it, I should just use $form right away? –  dragonmantank Mar 23 '12 at 15:55
    
Yep, that's the way it's done throughout Drupal core and contrib modules (and they way I've always done it) so I think it's definitely the right way to go –  Clive Mar 23 '12 at 15:56
    
I've seen it both ways. It looks like $form is empty when it comes into the builder function, but I changed it from the straight return and still not getting the theme stuff called. –  dragonmantank Mar 23 '12 at 16:07
    
Ah ok, good to know thanks. Have you tried clearing the caches? –  Clive Mar 23 '12 at 16:37
2  
I just popped your code into a text editor and you have a syntax error...in mymodule_form the line if(!empty($form_state['page_num'] && $form_state['page_num'] == 2) { is missing a closing parenthesis on the empty statement. Is that just a mistake in the question or could it be causing your problem? –  Clive Mar 23 '12 at 17:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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