[development] theming in D7
Bob Hutchinson
hutchlists at midwales.com
Fri Oct 22 20:56:07 UTC 2010
Hi folks, I'm struggling with theming in D7.
In D6 I can do the following:
create a form
function mymodule_thingy_form(&$form_state) {
// build a form in the usual manner
// .....
return $form;
}
optionally create validate and submit functions
function mymodule_thingy_form_validate($form, &$form_state) {
// validate form elements
}
function mymodule_thingy_form_submit($form, &$form_state) {
// submit the form info etc
}
then I can render the form with
....
$output = drupal_get_form('mymodule_thingy_form');
return $output;
....
Now in order to control the rendering in a theme I can add an entry to the
theme registry
function mymodule_theme() {
....
'mymodule_thingy_form' => array(
'arguments' => array(
'form' => NULL,
),
),
....
}
and create a function
function theme_mymodule_thingy_form($form) {
$output = drupal_render($form);
return $output;
}
The form will be automatically fed through the theme function, providing the
name is the same.
In D7 I get somewhat different behaviour:
If I do not create an entry in the theme registry, all works as expected, but
if I do, things start to behave oddly.
function mymodule_theme() {
....
'mymodule_thingy_form' => array(
'variables' => array(
'form' => NULL,
),
),
....
}
and create a function
function theme_mymodule_thingy_form($variables) {
$form = $variables['form'];
$output = drupal_render($form);
return $output;
}
The form is not fed through the theme automatically and is not rendered at
all, I see 'Array' where the form should be.
So I theme it explicitly with:
....
$form = drupal_get_form('mymodule_thingy_form');
$output = theme('mymodule_thingy_form', array('form' => $form));
return $output;
....
This results in no form visible at all, though there are mangled traces of it
in the source html.
So I use dd($form) to find out what is going on and discover that it is going
through the theme function twice and is being mangled on the second time
through, so I modify the theme function.
function theme_mymodule_thingy_form($variables) {
$form = $variables['form'];
$output = (is_array($form) ? drupal_render($form) : $form);
return $output;
}
Phew! my form is rendered.
This looks like a dreadful hack to me, what should I be doing?
Any pointers would be most welcome.
--
-----------------
Bob Hutchinson
Midwales dot com
-----------------
More information about the development
mailing list