Is it intended that when you place a markup field inside a collapsible fieldset that the markup is displayed even when the fieldset is collapsed?

If so, how would I place some markup inside the fieldset so that it is only shown when the fieldset is visible?

...Richard.

Comments

Richard Archer’s picture

'#type' => 'item'

CnnmnSchnpps’s picture

since you answered your own question, mind if i ask you one of my own?

could you point me to an example of collapsable fieldsets in use? it sounds like it could be useful at some point

thx

rschwab’s picture

The markup element type seems to be buggy in a different way as well. I have several different profile sections that appear on multiple pages. As an example, lets call two of my sections 'Basic Information' and 'Links'. If I use the form alter hook to add markup to my profile edit form like so:

function mymodule_form_user_profile_form_alter(&$form, &$form_state) {
  $form['Basic Information']['profile_new_field']['#type'] = 'markup';
  $form['Basic Information']['profile_new_field']['#value'] = 'my html here';
}

This markup will now appear on every profile edit page, including Edit Account, Edit Basic Information, and Edit Links. Yet my code clearly shows this markup to be within the Basic Information subsection. BUG!

The solution is to use different parts of the Form API. I personally wanted a return to profile link (because of the way my profile edit sections are set up, don't worry about it). So I used #suffix instead, like so:

function mymodule_form_user_profile_form_alter(&$form, &$form_state) {
  $form['Basic Information']['#suffix'] = 'my return link html here';
}

This will add the link to the end of the fieldset, and bypasses the bugs in the Markup type.

- Ryan

jcmc’s picture

Hello,
Here a example, so as it should be.

you must only change hook_form_alter() to your_module_name_form_alter();

function <strong>hook</strong>_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_profile_form') {
    $form['test_fieldset'] = array(
      '#type' => 'fieldset',
      '#title' => t('Title of the fieldset'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#access' => user_access(TRUE),
      '#weight' => 30,
    );

    $markup_content = '<div><a href="#">a link</a></div>';
    $markup_content .= '<div>normal text</div>';

    $form['test_fieldset']['markup_field'] = array(
      '#type' => 'markup',
      '#title' => $markup_content,
    );
  }
}
rschwab’s picture

Not really true. You can use either the generic hook_form_alter and include the form id as you posted above, or you can use a specific hook_form_alter with a function name built like so:

modulename + form + form id + alter

In my example, because I was targeting the user_profile_form, my function name ended up being:

mymodulename_form_user_profile_form_alter()

This function successfully added some markup to my form, but using either the markup or item element type gave me the problems I detailed in my post above. Only by adding my markup as a #suffix of an existing element did it work the way I had intended.

- Ryan

jcmc’s picture

Yes you have right,

Here a example code I tested and work.

- rendered form in the markup form element
- rendered link
- and rendered html code

function gallery_assist_form_user_profile_form_alter(&$form, $form_state) {
  $user = user_load($form['#uid']);
  $avatar = $user->picture ? $user->picture : 'http://pruebas.puntolatinoclub.de/sites/default/files/smiley85x85.png';
  $my_form['account'] = array(
    '#type' => 'textfield',
    '#title' => 'my test edit account',
  );
  $my_form['basics'] = array(
    '#type' => 'textfield',
    '#title' => 'my test edit basic information',
  );
  $my_form['links'] = array(
    '#type' => 'textfield',
    '#title' => 'my test edit links',
  );

  $form['Account']['profile_new_field']['#type'] = 'markup';
  $form['Account']['profile_new_field']['#value'] = drupal_render($my_form['account']) .'<div class="clear-block">a form plus html text example</div>';
  $form['Basic Information']['profile_new_field']['#type'] = 'markup';
  $form['Basic Information']['profile_new_field']['#value'] = drupal_render($my_form['basics']) .'<div class="clear-block">a form plus a image<br /><img src="'. $avatar .'" alt="'. $user->name .'" title="'. $user->name .'" /></div>';
  $form['Links']['profile_new_field']['#type'] = 'markup';
  $form['Links']['profile_new_field']['#value'] = drupal_render($my_form['links']) .'<div class="clear-block">a form plus a back link<br />'. l('RETURN TO PROFILE', 'user/'. $user->uid) .'</div>';
}

Juan Carlos