2

In my module I have a function that returns a form:

function mymodule_myform($form, &$form_state) {
    $form['fs_refine'] = array('#type' => 'fieldset');
    $form['fs_refine']['refine'] = array(
        '#type' => 'checkboxes',
        '#theme' => 'toggles',
        '#title' => t('Refine results by category:'),
        '#options' => array(
            1 => t('Cat 1'), 
            2 => t('Cat 2'),
            3 => t('Cat 3')),
        '#default_value' => array(1, 2, 3)
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit')
    );
    return $form;
}

I render the form with:

drupal_render(drupal_get_form('mymodule_myform'));

I have the following functions in my module:

function theme_toggles($vars) {
    die;
}

function mymodule_theme_toggles($vars) {
    die;
}

function mymodule_toggles($vars) {
    die;
}

I want to call a theme function for my checkboxes form element so that I can customise the HTML output of each checkbox. My theme functions are obviously not being called, because my page would by dying if one of them was.

How do I fix this and theme my form elements using #theme?

  • function theme_toggles($vars) { die; } should work. * Clear cache * Instead die, put print 'something';exit; – Vimalnath Apr 17 '13 at 7:15
1

OK, so you need several things combined in order for this to work:

  1. #theme => unique_name_for_theme_function on the form element.
  2. In your template.php mytemplate_theme() function you need to return an array something like: array( 'unique_name_for_theme_function' => array('function') => 'myfunc') );. This was the bit I was missing.
  3. Then in your template.php you need to add a myfunc. I found it easiest to just google "Drupal theme_form_element", "Drupal theme_checkboxes" or whatever, and then copy paste the code I found in to a mytemplate_theme_checkboxes function which I then modified to suit my needs.
  4. Then you need to flush cache for Drupal to find your theme function.
|improve this answer|||||

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.