1

I want to unset a fieldset programmatically which is available in the content type. I tried to unset like this in hook_form_alter,

 unset($form['field_first_deposit']);

But it doesn't work, where as hiding individual fields works in the same format. I tried hide() too , but still it doesn't work.

Any help will be much appreciated and Thanks in advance.

4 Answers 4

3

The mentioned solution ['#access'] = false will be a possible solution, but if it's not working you may try:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function YOUR_MODULE_form_FORM_ID_alter(&$form, &$form_state) {

  $form['#after_build'][] = 'YOUR_MODULE_form_FORM_ID_after_build';

}

/**Replace MACHINE_NAME with your fieldset group machine name.**/

function YOUR_MODULE_form_FORM_ID_after_build(&$form, &$form_state) {

  // Hide the B2B group.
  field_group_hide_field_groups($form, array('group_MACHINE_NAME'));

  return $form;
}
4
  • Its still showing the fieldset after adding the hook_form_FORM_ID_alter and _after_build() also.
    – Ram
    May 28, 2015 at 6:06
  • Is the FORM ID correct may be its not working because of the form id.
    – Sidharth
    May 28, 2015 at 6:12
  • you can use GET FORM ID module to check that
    – Sidharth
    May 28, 2015 at 6:17
  • I haven't used the recent version of field groups module and field_group_hide_field_groups() is not available in that version. Now updated the field groups version and seems to be working fine.
    – Ram
    May 28, 2015 at 6:45
1

The Drupal form API has the #access element for fields that toggles visibility. If you need hide a fieldset, you'll need to do both.

$form['field_first_deposit']['#access'] = false;
hide($form['field_first_deposit']);
3
  • 1
    I tried that option also and it doesn't seems to be working.Its showing the fieldset.
    – Ram
    May 28, 2015 at 5:11
  • It looks like you have to do both hide() & #access.
    – Shawn Conn
    May 28, 2015 at 5:27
  • 1
    Having both also still it doesn't help.
    – Ram
    May 28, 2015 at 5:44
1

You can try below code :

$form['field_first_deposit']['#access'] = '0';

It will hide the field

0

For Drupal 8/9 this works

$form['field_first_deposit']['#access'] = false;

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.