If you use code similar to the following one to define the items shown in a form, in a form builder, the rendered form contains just the markup element; the textfield will not be rendered.
$form['html'] = array(
'#type' => 'markup',
'#markup' => '<h2>My Heading</h2>',
'#tree' => true,
);
$form['html']['element'] = array(
'#type' => 'textfield',
'#title' => 'Foo',
);
The reference page for the "markup" element reports the "#tree" property as supported by the "#markup" element. The expected result would be that the child items of that element are rendered too.
Comments
Comment #1
darrell_ulm commentedThis appears fixed.
Was drupal_render() used in the above example? I think the docs mention that #markup is meant to be used within fieldsets.
Here is a test I ran, which is a slight modification of yours, and the #tree and it appears to work with a field set. However, perhaps this is the way that #tree was meant to work so it could be used without field sets.
The test:
Also from the Drupal online manual see:
http://drupal.org/node/751826
http://drupal.org/node/48643
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
Also:
Hope that solves it.
Comment #2
bleen commented@darellulm .. your small change to the example in the original post is a fundamental one. kiamlaluno's whole point is that an element of #type = markup should obey #tree just like any other element. Your example shows that #tree is working for #type = fieldset, but that is not the issue being raised here.
Comment #3
darrell_ulm commentedRight I see that. I wonder if the intent is that #tree should only work with fieldsets or it should be able to work with anything. Assuming now it is supposed to work with other, ?any?, element types.
Comment #4
bleen commented#tree definitely works with other element types that are not fieldsets...
Comment #5
avpadernoJust to make clear to what I am referring, I created a test module containing the following code:
When I visit the page it defines, this is what I see, in Drupal 8.
#markupis not supposed to be used only inside field sets, nor is the#treeproperty supposed not to be used within#markup, as bleen18 already said.Comment #6
darrell_ulm commentedTrue enough.
And a test I ran:
And it shows:
It does not show the second element.
Also:
Shows only:
So it would seem these non fieldset elements are acting similar.
Comment #7
valthebald#tree is not intended to tell Forms API that element has children. I.e. with the last 8.x, both
and
have the same effect: checkbox titled I am a child appears on form.
For the reference, please check http://drupal.org/node/48643
Comment #8
Kazanir commentedIn that case, this is an outstanding bug in D7. I have just tested it with the following code:
Only the first form element here prints -- its child element is not rendered.
Comment #9
valthebaldtldr: fix is non-trivial for D7, should be won't fixed
In detail:
in function drupal_render(common.inc):
#markup elements have pre_render function drupal_pre_render_markup(), that copies $element['#markup'] to $elements['#children']. Existing $elements['#children'] prevents children from being rendered. Bingo!
It is possible to try and fix the current behavior and, for example, use #theme_wrappers instead of #pre_render technique for markup elements. That's how fieldset elements are being rendered.
I think the reason for the current behavior is simple - speed. And seriously - use case of #markup element having children sounds too specific to shake rendering basics.
Comment #10
avpaderno@valthebald I am fine with that, but in that case what shown in this page should be changed, since it shows that the only form element that doesn't use #tree is value. Basing on your explanation, that is not true.
Comment #11
Kazanir commentedThis isn't really what #tree is for, but the documentation should clearly indicate that this is the case then.
Comment #12
valthebaldChanging topic and component. (should this be moved to another queue? webmasters or something?)
Comment #17
bleen commented... I think valthebald fell asleep on his keyboard :)
Comment #18
jhodgdonOK... There are really a couple of different things going on here:
a) Several of the comments above illustrate a common misunderstanding of what #tree does. #tree affects how the values are passed to validate/submit functions in $form_state -- whether they are given to you in a flat array or a hierarchical array. #tree does not affect how the form is presented on the page in any way.
b) Another misunderstanding is about whether you can nest form elements inside other form elements in a $form array. You cannot do that in general (at least in Drupal 7) -- you can only do that for elements that support nesting, such as fieldsets and vertical tabs. Markup elements do not support nesting, as noted above.
So... Since both of these misunderstandings have come up, probably some documentation needs to be improved:
1. The Form API reference #tree section should explain what #tree does. Currently it's totally useless, and to make matters worse, it points to an obsolete archived page for more information. So that needs to be fixed.
https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.h...
The Form API reference is in the Documentation git repository, so I'm moving this issue there.
2. The "more information" link there should go to the Quickstart guide, which says it is for Drupal 6 but really applies to both 6 and 7:
https://drupal.org/node/751826
3. That quickstart guide should explain the difference between having a fieldset with #tree being TRUE and one with #tree being FALSE (the effect on the values array), so that people have some hope of not being confused about #tree.
4. And maybe it should explain that you can't nest arbitrary form elements, just ones that support nesting.
Comment #19
star-szrSee also #2012818: Remove #type 'markup'.
Comment #19.0
star-szrMade the report clearer.
Comment #20
solideogloria commentedThe documentation is still unhelpful. Have any documentation changes been made for this?
There is nothing in the docs about which elements support nesting, either.
Comment #21
quietone commentedThis is for the Drupal 7 Form API reference, so this is now a won't fix.
Comment #22
quietone commentedWork on documentation for supported versions of drupal is at #2486967: [meta] Move/Create Form Element Documentation and #3313535: [Meta] Improve documentation for Render and Form Elements.
Comment #23
avpaderno