Posted by JeebsUK on November 16, 2009 at 11:48am
Hi all,
I want to override the theme_form_element function which is easy enough in itself, but I only want to do it in certain circumstances. As I understand it you can set #theme on a form element which is picked up by drupal_render, so if I have a textfield I can set a custom theme function on the properties of the form element, but then theme_textfield calls theme('form_element').
Would I have to create my own theme_textfield (or other theme functions) which then perform an if statement to decide whether to use the original theme_form_element or my custom one? I can't see an easier way to do it.
Comments
This looks like an old post,
This looks like an old post, but Im also wanting to do the same thing.
Basically I want to do overide the form element function
_form_element($element, $value) {
(possible conditonal if statment)
}
But when i do so, it is site wide, is there a way to place conditionals in this function so it only limited to a specific form?
Try this
You could overwrite the function in your theme and examine the element for a specific key. For example:
In your theme:
function mytheme_form_element($element, $value) {
...
if(isset($element['#mykey'])) {
...do something...
}
...
//Remember that you need return the output string (You could call to the original 'theme_form_element')
}
In your form construction function:
function my_form($form_state) {
...
$form['myelement'] = array(
...
'#mykey' => 'value',
...
);
...
}
Sorry if my english is bad, I'm spanish
Subscribing...
Subscribing...
Use form theme functions
Depending upon your use case, you could use the built in form theming system. This allows you to override form element theming on a per-element basis. This is made slightly more complex due to the fact that a form element theme function calls the wrapper function 'theme_form_element'.
For example, when you are modifying the wrapper on a textfield, there are two theme functions that build the display. First, the array element is passed to theme_textfield, which produces the HTML input element. Then, within that function, theme_form_element is called. This wraps the basic input element, adding in things like labels, spans for required fields, and wraps the whole lot in a div.
So, when building your override system, you need to override both functions, even if you are essentially happy with the theme_textfield function, because you need to define a new wrapper function.
All of this effort means that you can now modify your form array (possibly via hook_form_alter) to conditionally theme form items.
So, there are essentially three steps:
You are on the right track
I found this lullabot article which give sort of a broad overview of a cool trick in the wp_comments module.
Basically you can sort of flag your form elements in hook_form_alter() with a boolean and check for the flag when overriding theme_form_element(). Very handy!
$form['name']['#themethis_name'] = TRUE;Check out some wp_comments code which is a great example. Of course this means building a module instead of just using a normal template.php override, but it is pretty easy and worth it.
Example
Similar approach here with a nice, straightforward example: http://hokuten.net/2010/drupal-6-hiding-drupal-form-labels/