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 -----------------
On 10/22/2010 1:56 PM, Bob Hutchinson wrote:
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, ), ),
In D7 the above should actually be: 'mymodule_thingy_form' => array( 'render element' => 'form',
.... } and create a function function theme_mymodule_thingy_form($variables) { $form = $variables['form']; $output = drupal_render($form); return $output; }
You should use either render() or drupal_render_children() rather than drupal_render(). So far as I could figure out, drupal_render() should no longer be used inside theme functions. You use render() to render a specific element, and drupal_render_children() to render all the children but NOT the element (which is what you want when attempting to render $form since that is currently being rendered).
On Saturday 23 October 2010, Earl Miles wrote:
On 10/22/2010 1:56 PM, Bob Hutchinson wrote:
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,
),
),
In D7 the above should actually be:
'mymodule_thingy_form' => array( 'render element' => 'form',
.... } and create a function function theme_mymodule_thingy_form($variables) {
$form = $variables['form']; $output = drupal_render($form); return $output;
}
You should use either render() or drupal_render_children() rather than drupal_render(). So far as I could figure out, drupal_render() should no longer be used inside theme functions. You use render() to render a specific element, and drupal_render_children() to render all the children but NOT the element (which is what you want when attempting to render $form since that is currently being rendered).
OK, thanks, I will try this. -- ----------------- Bob Hutchinson Midwales dot com -----------------
On Saturday 23 October 2010, Earl Miles wrote:
On 10/22/2010 1:56 PM, Bob Hutchinson wrote:
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,
),
),
In D7 the above should actually be:
'mymodule_thingy_form' => array( 'render element' => 'form',
.... } and create a function function theme_mymodule_thingy_form($variables) {
$form = $variables['form']; $output = drupal_render($form); return $output;
}
You should use either render() or drupal_render_children() rather than drupal_render(). So far as I could figure out, drupal_render() should no longer be used inside theme functions. You use render() to render a specific element, and drupal_render_children() to render all the children but NOT the element (which is what you want when attempting to render $form since that is currently being rendered).
I ran some trials using form_example from http://drupal.org/project/examples I found that indeed I should not use drupal_render() inside theme functions, drupal_render_children() works fine. I also found that using drupal_get_form() within a function (as opposed to in a menu) requires the output of drupal_get_form() to be run through render(), otherwise the form is not complete. The theme (defined with 'render element') is picked up automatically, the same as it is when calling drupal_get_form() directly in a menu $item. Thanks for your help. -- ----------------- Bob Hutchinson Midwales dot com -----------------
A form theming example would be a nice addition to the form_example if you'd be interested in posting a patch. Thanks, -Randy On Sun, Oct 24, 2010 at 10:45 AM, Bob Hutchinson <hutchlists@midwales.com>wrote:
On Saturday 23 October 2010, Earl Miles wrote:
On 10/22/2010 1:56 PM, Bob Hutchinson wrote:
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,
),
),
In D7 the above should actually be:
'mymodule_thingy_form' => array( 'render element' => 'form',
.... } and create a function function theme_mymodule_thingy_form($variables) {
$form = $variables['form']; $output = drupal_render($form); return $output;
}
You should use either render() or drupal_render_children() rather than drupal_render(). So far as I could figure out, drupal_render() should no longer be used inside theme functions. You use render() to render a specific element, and drupal_render_children() to render all the children but NOT the element (which is what you want when attempting to render $form since that is currently being rendered).
I ran some trials using form_example from http://drupal.org/project/examples I found that indeed I should not use drupal_render() inside theme functions, drupal_render_children() works fine. I also found that using drupal_get_form() within a function (as opposed to in a menu) requires the output of drupal_get_form() to be run through render(), otherwise the form is not complete. The theme (defined with 'render element') is picked up automatically, the same as it is when calling drupal_get_form() directly in a menu $item.
Thanks for your help.
-- ----------------- Bob Hutchinson Midwales dot com -----------------
-- Randy Fay Drupal Module and Site Development randy@randyfay.com +1 970.462.7450
On Sunday 24 October 2010, Randy Fay wrote:
A form theming example would be a nice addition to the form_example if you'd be interested in posting a patch.
I'll have a go!
Thanks,
-Randy
On Sun, Oct 24, 2010 at 10:45 AM, Bob Hutchinson
<hutchlists@midwales.com>wrote:
On Saturday 23 October 2010, Earl Miles wrote:
On 10/22/2010 1:56 PM, Bob Hutchinson wrote:
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,
),
),
In D7 the above should actually be: 'mymodule_thingy_form' => array(
'render element' => 'form',
.... } and create a function function theme_mymodule_thingy_form($variables) {
$form = $variables['form']; $output = drupal_render($form); return $output;
}
You should use either render() or drupal_render_children() rather than drupal_render(). So far as I could figure out, drupal_render() should no longer be used inside theme functions. You use render() to render a specific element, and drupal_render_children() to render all the children but NOT the element (which is what you want when attempting to render $form since that is currently being rendered).
I ran some trials using form_example from http://drupal.org/project/examples I found that indeed I should not use drupal_render() inside theme functions, drupal_render_children() works fine. I also found that using drupal_get_form() within a function (as opposed to in a menu) requires the output of drupal_get_form() to be run through render(), otherwise the form is not complete. The theme (defined with 'render element') is picked up automatically, the same as it is when calling
drupal_get_form()
directly in a menu $item.
Thanks for your help.
-- ----------------- Bob Hutchinson Midwales dot com -----------------
-- ----------------- Bob Hutchinson Midwales dot com -----------------
On Sun, Oct 24, 2010 at 7:45 PM, Bob Hutchinson <hutchlists@midwales.com>wrote:
I ran some trials using form_example from http://drupal.org/project/examples I found that indeed I should not use drupal_render() inside theme functions, drupal_render_children() works fine.
It's ok to use drupal_render on $form's sub-elements inside $form's theme function, however you can not use it on the $form array as it causes an infinite loop.
On Sunday 24 October 2010, ufuk bayburt wrote:
On Sun, Oct 24, 2010 at 7:45 PM, Bob Hutchinson <hutchlists@midwales.com>wrote:
I ran some trials using form_example from http://drupal.org/project/examples I found that indeed I should not use drupal_render() inside theme functions, drupal_render_children() works fine.
It's ok to use drupal_render on $form's sub-elements inside $form's theme function, however you can not use it on the $form array as it causes an infinite loop.
he, that explains why I ran out of memory ;-) -- ----------------- Bob Hutchinson Midwales dot com -----------------
participants (4)
-
Bob Hutchinson -
Earl Miles -
Randy Fay -
ufuk bayburt