Skip to content

Instantly share code, notes, and snippets.

@acao
Last active August 29, 2015 14:02
Show Gist options
  • Save acao/6fbe6d25a504b63b946b to your computer and use it in GitHub Desktop.
Save acao/6fbe6d25a504b63b946b to your computer and use it in GitHub Desktop.
A handy example of how to hide non-required fields from a form - in this case, on a profile2_regpath altered user_register_form
<?php
function MYMODULE_form_alter(&$form, &$form_state, $form_id){
if ($form_id == 'user_register_form') {
$fields = array_filter($form['profile_PROFILE_TYPE_NAME'], "_filter_required_fields");
foreach ($fields as $field_name => $field) {
$form['profile_PROFILE_TYPE_NAME'][$field_name]['und']['#access'] = false;
}
}
}
function _filter_required_fields($field){
if (is_array($field) && isset($field['und'])){
if (!isset($field['und']['#required']) || $field['und']['#required'] == false){
return $field;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment