After lots of trial I found out that drupal_render() won't work on a single element. Instead one should use theme('element_type', $element);

That is not confusing in some cases, but in the context of a theming forms and using '#theme' override, one would normally use drupal_render() to render any element they want, only to discover it won't work unless your element has children.

Actually this might not be a matter of documentation only. I think it's far better to correct this bad behavior. Because in some cases you might not know if the element in hand is a single one or has children.

Also, when setting a '#theme' override on the element, drupal_render() changes the element's '#type' to 'markup' before passing the element to the '#theme' function so it has no way to detect the element type on real time in order to use the appropriate theme() function.

Comments

ssemaganda’s picture

Project:Documentation» Drupal core
Version:» 7.x-dev
Component:Developer Guide» documentation
Roman S’s picture

Just ran into this issue as well in 5.10. Being used to calling drupal_render() within the #theme function, it was a surprise when that didn't work for a single element, e.g. $form['mail']. Similar to alienbrain I had to use theme('type', $element), however it is a problem when the same #theme value is used for multiple field types. Is there a solution that we might be overlooking?

Stuart Greenfield’s picture

I am starting to explore D7 and have one project where I need to define a custom element. Under D6 it was easy - the element automatically got passed to the right theme by drupal_render.

Under D7 I can't get it to work as the theme remains unassigned, so the element is never output.

Is this intentional? If so, what is the correct way to define an element, and its associated theme?

Ok - I think I got it. I see that in system_elements() it is now necessary to include the theme definition explicity on the element, rather than allowing the system to "fallback" and assign it automatically.

I'm guessing that as D7 stabilises and documentation gets updated this will be made clear, but it is a fairly fundamental change.

kriskhaira’s picture

Status:Active» Needs review

I've documented this here in the Converting 6.x themes to 7.x section of the Theming Guide.

shaper_pmp’s picture

Also, when setting a '#theme' override on the element, drupal_render() changes the element's '#type' to 'markup' before passing the element to the '#theme' function so it has no way to detect the element type on real time in order to use the appropriate theme() function.

Still experiencing this problem with custom elements in Drupal 6.10 - they're defined fine in hook_elements(), and the #process function correctly converts the element's #type to 'file', but then by the time the element is passed to the theme_element_name function the #type has been set to 'markup'.

The only workaround I've found is to disable themeing for the whole custom element (remove reference to it from hook_theme, remove the theme_element_name() function and remove the custom element's #theme value from hook_elements()), and just use the #process function to convert the custom element into form elements Drupal already recognises... but obviously this is a far from ideal solution.

moshe weitzman’s picture

Version:7.x-dev» 6.x-dev
Category:bug» support
jhodgdon’s picture

OK, I'm confused. Is there a need for better documentation somewhere, and if so, exactly where? If so, please make this a "bug report" and say where documentation is needed.

Or is this a support request -- in which case I think the questions have been answered and it can be closed?

jhodgdon’s picture

Status:Needs review» Closed (fixed)

OK, it's a support request, and I'm closing it... please reopen if it's a real documentation bug.