2

I cannot seem to get "Locale settings" fieldset to collapse using hook_form_FORM_ID_alter().

Here is what I've tried so far.

function fsrsys_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
    $form['timezone']['#type'] = 'fieldset';
    $form['timezone']['#collapsible'] = TRUE;
    $form['timezone']['#collapsed'] = TRUE;  

    $form['#after_build'][] = 'user_profile_form_afterbuild';
}

function user_profile_form_afterbuild($form, $form_state) {
    $form['timezone']['#collapsed'] = TRUE; 
    //dd($form['timezone']['#collapsed']);  // <-- shows '1'
    return $form;
}

The fieldset is still 'open.'

For good measure, I've cleared both the Drupal cache and the browser cache twice.

What am I doing wrong?

1
  • The hook works perfectly for me when calling from template.php. Possible workarounds: switch to Garland if custom theme in use; find a module that may invoke the hook and set the higher weight for yours.
    – P4tient
    Commented Aug 4, 2011 at 23:06

2 Answers 2

1

This is a bit of a hack but it worked for me.

My desired effect was to have the Locale Settings start out collapsed and be expandable, every other solution out there suggests just "turning it off". I still need to use the Locale settings though. Anyway!!! Execute this jquery on the edit user profile page ( I inserted it using a hook_form_alter):

(function($){
  $(document).ready(function(){
    $('#edit-timezone .fieldset-wrapper').toggle();
    $('#edit-timezone').addClass('collapsed');
  });
})(jQuery);
2
  • 1
    This might not work when validation is triggered. You might want to add the script using $form['#after_build'] instead.
    – Bart
    Commented Oct 7, 2011 at 13:22
  • True, there are lots of different ways to throw this script on your page. My personal fave is to use drupal_add_js with drupal_get_path and add a js file that is inside my module. inside the JS file at the bottom is a document ready that calls function defined before it.
    – LSU_JBob
    Commented Oct 7, 2011 at 15:13
1

You can just add a class collapsed to the attributes array within the form element. So for locale settings, in your form after build function you can use $form['timezone']['#attributes']['class'][] = 'collapsed';

1
  • Wow, that's strange... Your solution works perfectly, but when I tried adding the collapsed class via $form['timezone']['#attributes'] = array('class => array('collapsed')); which works fine for other fieldsets, timezone would end up collapsed and unopenable.
    – Simo
    Commented May 22, 2018 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.