Why isn't my custom form element theme function being called? In my module I have a function that returns a form:

function mymodule_myform($form, &amp;$form_state) {
    $form['fs_refine'] = array('#type' =&gt; 'fieldset');
    $form['fs_refine']['refine'] = array(
        '#type' =&gt; 'checkboxes',
        '#theme' =&gt; 'toggles',
        '#title' =&gt; t('Refine results by category:'),
        '#options' =&gt; array(
            1 =&gt; t('Cat 1'), 
            2 =&gt; t('Cat 2'),
            3 =&gt; t('Cat 3')),
        '#default_value' =&gt; array(1, 2, 3)
    );
    $form['submit'] = array(
        '#type' =&gt; 'submit',
        '#value' =&gt; 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?
OK, so you need several things combined in order for this to work:


#theme =&gt; unique_name_for_theme_function on the form element.
In your template.php mytemplate_theme() function you need to return an array something like: array( 'unique_name_for_theme_function' =&gt; array('function') =&gt; 'myfunc') );. This was the bit I was missing.
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.
Then you need to flush cache for Drupal to find your theme function.