0

I understand how to edit a specific content type with form_alter

my_module_form_node_form_alter(&$form, $form_state) {

  $type = $form['#node']->type;

  if ($type == 'my_content_type') {

  } 
}

I would like to hide all collapsible fieldsets EXCEPT scheduling. How do I only target these other fieldsets to hide?

http://screencast.com/t/ozHu5esPGc

1

Dull ax approach would be to cycle through your $form looking for fieldsets and unsetting (now setting #access to FALSE) all but the one you want to keep, eg, something along the lines of...

// drupal_set_message('<pre>' . print_r($form, TRUE) . '</pre>');
foreach($form as $key => $field) {
  if ($key != 'field_FIELDNAME' && is_array($field)  && $field['#type']=='fieldset' && $field['#collapsible']) {
    // unset($form[$key]);          // ADDITION/EDIT: '#access' below is a better/working approach
    $form[$key]['#access'] = FALSE; // than unset if required fields are in the fieldset :)
  }
}

...with the drupal_set_message() helping to get the field_FIELDNAME for your scheduling fieldset.

Just realize that without further logic, you would never be able to edit any fields inside these fieldsets again.

Another option might be to check out the nodeformcols module that makes lengthy node forms a bit nicer and does contain this functionality out of the box for both fields as well as fieldsets.

  • What do you mean "you would never be able to edit any fields inside these fieldsets again." I don't want the user to edit the fields inside these ever again, on this particular node type. That is the point of hiding them. – albo Nov 19 '13 at 16:41
  • well, just pointing out what might not be obvious. perhaps you would want an admin to be able to change them in the future. if so, you might want to put in logic for that (based on $user->uid or role). you also might want to make sure that logic is in there so that this only happens on the edit page, not the initial node add, etc. – Jimajamma Nov 19 '13 at 16:45
  • see also change to use '#access' instead of unset()ing. I just tried it the first way and it screws up if you have required fields in the fieldset :( – Jimajamma Nov 19 '13 at 16:54
  • I this is not the solution but had to pick something. Just wanted to hide certain fields on one content type. I used CSS advanced selectors. There should be a module for this to make a simple node edit for users – albo Dec 31 '13 at 15:17
0

by using #attached property you can add js to this form in this form alter only. And using jQuery .hide() function you can hide the divs of those fieldsets that you don't require. This way entire fieldset will get hidden

  • But how do you rarfe specific fields – albo Dec 31 '13 at 15:17
  • As Drupal generates lot of markup, find the CSS class of the div tag encloses the fieldset you want to hide. Please note that this will hide the fieldset just aesthetically, in actual form the fields will be present. – ruby Jan 2 '14 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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