Drupalist

Hiding the clutter on "Create content" and other forms

One of the first things that struck me about the Drupal 6 out-of-the-box experience is that there are an overwhelming number of choices when one clicks Create content and creates ones's first Story and Page nodes. The first user account is privileged and hence you see many options a typical user would not when you are using the first account.

When I moved on to custom types and considering actual users of Composers' Village there were still fields on some forms that I wanted to hide when a typical user was logged in.

Here is some Drupal 6 sample code that turns off access to the personal contact form settings, so users of Composers' Village cannot turn off their own contact forms. The sample code also turns off access to a hidden field (filed_o0) of my CCK type Tune that is set programmatically later in the flow and hence is not meant for humans at all. The strategy is to implement hook_form_alter and set the "#access" property to false for elements you wish to turn off.

/**
 * An implementation of hook_form_alter
 */
function composers_village_form_alter(&$form, $form_state, $form_id) {
  global $user;
 
  $is_admin = false;
  if (user_access('access administration pages')) {
    $is_admin = true;
  }
 
  if ($form_id == 'user_profile_form' && !$is_admin) {
    // make a typical user unable to hide their personal contact page
    if (isset($form['contact'])) {
      $form['contact']['#access'] = false;
    }
  }
 
  // turn off all access to the owner field,
  //   since it is set by nodeapi in 'presave'
  if (isset($form['field_o0'])) {
    $form['field_o0']['#access'] = false;
  }
}

Comments

There's a module for that...

I'm here at Drupal Camp Western Massachusetts in a session called "25 Must Have Modules" by Katie Methe of Gravity Switch and she introduced us to Node Form Columns module that can hide anything on the node form. Nice tip!

Drupal SEO