Toggle #input_group on fields in Drupal 7x

Here's the code for iterating through all fields in a form and setting $element['#input_group'] = TRUE;
 
 
function hook_form_alter(&$form, $form_state, $form_id) 
{
 if ($form_id == 'nodetype1_node_form') 
 {
  iterateForm($form, 'setIgt');
 }
}

// Generic iterate function. Can be used with any callback.
function iterateForm(&$form, callable $callback) 
{
 foreach (element_children($form) as $key) 
 {
  $element = &$form[$key];
  $children = element_children($element);
  if (empty($children)) 
  {
   call_user_func_array($callback, array(&$element, &$key));
  }
  else 
  {
   iterateForm($element, $callback);
  }
 }
}

// Custom callback
function setIgt(&$element, &$key) 
{
 if ($element['#type'] == 'textfield')
  $element['#input_group'] = TRUE;
}

Comments