Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By thx538 on
I'm new to drupal, developing a custom module and trying to add some [collapsible] custom fields in my node. So I added this code in my hook_view (before $node = node_prepare($node, $teaser);) :
...
if ($page) {
drupal_add_js('misc/collapse.js');
$node->content['Id'] = array(
'#type' => 'fieldset',
'#title' => t('myfieldset'),
'#weight' => -9,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$node->content['Id']['myfield'] = array(
'#value' => $node->myfield,
'#parent' => 'Id',
'#weight' => -9,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
*/
}
... but for some reasons the generated HTML keeps myfield outside of the collapsible area.
The above code generates
.
So I ended up tweaking with this :
...
if ($page) {
drupal_add_js('misc/collapse.js');
$node->content['Id'] = array(
'#type' => 'fieldset',
'#title' => t('Artist Details'),
'#weight' => -9,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$data = 'First Name: <b>'.$node->firstname.
'</b> Last Name: <b>'.$node->lastname.'</b> ';
$data .= $node->toto == '' ? '' : ' Alias: <b>'.$node->toto.'</b>';
$node->content['Id']['myfield'] = array(
'#prefix' => '<div class="fieldset-wrapper">',
'#title' => t('This is my field'),
'#value' => $node->myfield,
'#parent' => 'Id',
'#weight' => -8,
'#suffix' => '</div>',
);
}
I'd be curious to know whether it's a bug or if i missed something.
Thanks for your help
Comments
You shouldn't need to define
You shouldn't need to define anything *but* the field set itself as collapsible. content['Id']['myfield'] already tells the forms API that the parent is 'Id' (or should).
<div> is needed
After some further testing I discovered that a "div" section is needed in the child field, probably for the JavaScript to work.
The following code works.
Try this one. It works fine
Try this one. It works fine for me.
Hope this will help
Cheers.
This is $node object, not forms!
naveenpl:
What you write does NOT work in this context because you are talking about $form. The OP is talking about $node objects. As the OP replied, using a
<div>
fixes the problem.