I want to override theme_form_element within a module. I know it can be done in the template.php file, but that will override the theme for all modules - I want this to be local to one module. Is this possible? I have tried to use a template.php file in my module, but that apparently doesn't work. I have also been trying to register a theme hook within my module, but if it can be done, I haven't figured out how to do it yet. If someone can even confirm that I can override the theme by registering a theme hook in my module, I can figure out how to do it. Or if someone can point me at how to override the theme_form_element from within a module, I would appreciate that as well. Thank you.

Comments

jaypan’s picture

I figured out how to do this. It turns out I actually wanted to override theme_radio($element), not theme_form_element(). I ended up doing it in the template.php file in my theme, using phptemplate_radio($element). Now, I didn't want to override all radio buttons on the site, only the radio buttons for that particular form element. It turns out that in $element, there is $element['#name'], and this holds the name of the form element that I used in the form api.

For example, I used:

$form['this_form'] = array
(
  '#type' => 'radios',
  '#title' => t(''),
  '#options' => array
  (
    t('radio_button1'),
    t('radio_button2'),
  )
)

This meant that in phptemplate_radio(), I could use the following conditional:

if($element['#name'] == 'this_form')
{
  // Process the radio buttons in the way I want them for that form
}
else
{
  // Process them normally (I copied the original code from theme_radio() on api.drupal.org)
}

I don't know if this is the ideal way to do this, and if someone can point me at a better way, please do - I'd rather learn the right way. But for the time being, that is what I did, and it worked!

Contact me to contract me for D7 -> D10/11 migrations.

jdl100’s picture

I wanted to override the way that the radio buttons were displayed in a webform. I couldn't just modify the css because I needed to not display the $element['#title'].

If there's a better way to do this, please let me know.

Jade
Learn Spanish
www.edufone.com