0

I have constructed a form manually using the Form API. I have one field in particular that needs to have it's markup changed a tad.

The #prefix/#suffix output should come immediately before/after the input control. I think the output currently looks like:

Can I override the themeing for a single field using something like:

$form['control'] = array(
  '#theme' => 'control_field',
  '#title' => 'Some title',
  '#type' => 'textfield'
);

Do I then implement MYTHEME_custom_theme in the templates.php???

Do you know of any exmaples I might use?

I tried adding:

function theme_control_field($variables) {

}

This did not seem to work...also I need control over all the markup for this field not just the field control but I need to relocate the prefix/suffix output as well!?!

Regards, Alex

  • Did you clear the cache first? I believe you need to do so when new functions are added to a module or theme. – Rick Oct 10 '13 at 14:33
  • I did yes... I don't understand why only the <input> element is removed when I add the '#theme' element to $form array -- I assumed the whole thing would be blanked (prefix, suffixe, title, description, etc) but those seem to still render -- just without the <input> – Alex.Barylski Oct 10 '13 at 14:47
3

You can have a complete custom HTML for a particular field by defining a #theme and setting #theme_wrappers with an empty value.

$form['control'] = array(
  '#title' => 'Some title',
  '#type' => 'textfield'
  '#theme' => 'YOURMODULE_control_field',
  '#theme_wrappers => array(),
);

Then Drupal needs to know about this new theme by implementing the HOOK_theme. You can either work with a template file or generate the HTML within the theme callback function

function YOURMODULE_theme()
{
    return array(
        'YOURMODULE_control_field' => array(
            'template' => 'templates/control-field', // if you want to have a tpl file, templates/control-field.tpl.php using the $element variable for your textfield element
            'render element' => 'element',
        ),
    );
}

// If you dont want to use the template file
function theme_YOURMODULE_control_field($variables)
{
    $element = $variables['element'];
    // $element is here your form textfield element, this function return HTML to generate
}
  • Excellent...I managed to achieve my desired effect by hooking MYTHEME_form_element() changing the markup on that one field and using default rendering on others but I still wanted to know how the above was possible so much appreciated. :) – Alex.Barylski Oct 11 '13 at 19:20

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.