You are here

function private_form_alter in Private 6

Same name and namespace in other branches
  1. 5 private.module \private_form_alter()
  2. 7.2 private.module \private_form_alter()
  3. 7 private.module \private_form_alter()

Implementation of hook_form_alter()

This module adds a simple checkbox to the node form labeled private. If the checkbox is labelled, only the node author and users with 'access private content' privileges may see it.

File

./private.module, line 132
A tremendously simple access control module -- it allows users to mark individual nodes as private; users with 'access private content' perms can read these nodes, while others cannot.

Code

function private_form_alter(&$form, $form_state, $form_id) {
  if ($form['#id'] == 'node-form') {
    $node = $form['#node'];
    $default = variable_get('private_' . $node->type, PRIVATE_ALLOWED);
    if ($default != PRIVATE_DISABLED || !empty($node->privacy)) {
      if (empty($node->nid)) {
        $privacy = $default > PRIVATE_ALLOWED;
      }
      else {
        $privacy = $node->private;
      }
      if (user_access('mark content as private') && $default != PRIVATE_ALWAYS) {
        $form['private'] = array(
          '#type' => 'checkbox',
          '#title' => t('Make this post private'),
          '#return_value' => 1,
          '#description' => t('When checked, only users with proper access permissions will be able to see this post.'),
          '#default_value' => $privacy,
        );
      }
      else {
        $form['private'] = array(
          '#type' => 'value',
          '#value' => $privacy,
        );
      }
    }
  }
  elseif ($form_id == 'node_type_form') {
    $node_type = (array) $form['#node_type'];
    $type = $node_type['type'];
    $form['workflow']['private'] = array(
      '#type' => 'radios',
      '#title' => t('Privacy'),
      '#options' => array(
        PRIVATE_DISABLED => t('Disabled (always public)'),
        PRIVATE_ALLOWED => t('Enabled (public by default)'),
        PRIVATE_AUTOMATIC => t('Enabled (private by default)'),
        PRIVATE_ALWAYS => t('Hidden (always private)'),
      ),
      '#default_value' => variable_get('private_' . $type, PRIVATE_ALLOWED),
    );
  }
}