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

followed by the value of myfield which stays outside the
.

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

cridenour’s picture

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).

thx538’s picture

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.

    $node->content['Id']['myfield'] = array(
    '#prefix' => '<div class="test">',
    '#value' => 'test',
    '#suffix' => '</div>',
    );
naveenpl’s picture

Try this one. It works fine for me.

	$form['showproperties'] = array(
		'#type' => 'fieldset',
		'#collapsible' => TRUE,
		'#collapsed' => FALSE,
                '#title' => t('Title if needed'),
	);	
	$form['showproperties']['secondfield'] = array(
 		'#type' => 'fieldset',
		'#collapsible' => TRUE,
		'#collapsed' => FALSE,
                '#title' => t('Title if needed2'),
      );
       $form['showproperties']['secondfield']['testform'] = array(
		'#type' => 'textfield',
                '#title' => t('Form title'),
		'#attributes' => array("style" =>"width:200px;"),
                '#default_value' => $value,
		'#required' => TRUE
      );	
      return $form;

Hope this will help
Cheers.

earwax’s picture

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.