Thursday, July 18, 2013

Drupal - Hiding field group and its Children

It came to me when I was trying to hide field_group on account page. Searching on I found the following solution using hook_form_alter
// Comment
/**
* Implements hook_form_FORM_ID_alter().
*/
function MODULE_form_node_form_alter(&$form, &$form_state) {
  global $user;

  // The 'internal' field groups should only be accessible for 
regional administrators.
  if (!in_array('regional administrator', $user->roles)) {
    foreach (array('group_action_internal', 'group_company_internal', 
'group_topic_internal') as $fieldgroup) {
      if (isset($form['#fieldgroups'][$fieldgroup]) 
&& isset($form['#group_children'])) {
        // Hide the field group itself.
        $form['#fieldgroups'][$fieldgroup]->format_type = 'hidden';
        // Hide the elements inside the field group.
        foreach ($form['#group_children'] as $field => $group) {
          if ($group == $fieldgroup) {
            $form[$field]['#access'] = FALSE;
          }
        }
      }
    }
  }
}
Source https://drupal.org/node/1324860


No comments:

Post a Comment