Drupal - Hide the "Administrative overlay" Checkbox on the User Edit Form

Category: 

Do you need to hide the 'Use the overlay for administrative pages' checkbox in the 'Administrative overlay' fieldset on the user edit form? Then look no further my child, this can be accomplished with hook_form_alter and an #after_build in your custom module.

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_profile_form') {
    // Attach a custom after build handler.
    $form['#after_build'][] = 'my_module_user_form_after_build';
  }
}

/**
 * An #after_build handler for the user_profile_form.
 */
function my_module_user_form_after_build($form, &$form_state) {
  global $user;
  // Hide the admin overlay check box from non admins.
  if (!in_array('admin user', $user->roles)) {
    $form['overlay_control']['#prefix'] = '<div style="display: none;">';
    $form['overlay_control']['#suffix'] = '</div>';
  }
  return $form;
}

Now you can sleep well knowing most users can't change this setting, unless they are an admin, like you, you cool S.O.B or they are sneaky with Javascript. This is a little whacky hacky, but it will work. Be wary of your advanced users that could manipulate javascript via the console, and change this setting for their account. But then again, if they know how to do that, then maybe it is OK if they want to toggle this on/off.

Comments

I have been searching all over for a solution for this. Thank you!

Add new comment