You are here

function private_form_alter in Private 7

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

Implements 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 153
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 (!empty($form['#node_edit_form'])) {
    $node = $form['#node'];
    $default = variable_get('private_' . $node->type, PRIVATE_ALLOWED);
    if ($default != PRIVATE_DISABLED || !empty($node->private)) {
      if (empty($node->nid)) {
        $privacy = $default > PRIVATE_ALLOWED;
      }
      else {
        $privacy = isset($node->private) ? $node->private : 0;
      }
      if (user_access('mark content as private') && $default != PRIVATE_ALWAYS) {
        if (user_access('administer nodes')) {
          $form['options']['private'] = array(
            '#type' => 'checkbox',
            '#title' => t('Make this post private'),
            '#attributes' => array(
              'title' => t('When checked, only users with proper access permissions will be able to see this post.'),
            ),
            '#default_value' => $privacy,
          );
        }
        else {
          $form['private'] = array(
            '#type' => 'checkbox',
            '#title' => t('Make this post private'),
            '#attributes' => array(
              'title' => t('When checked, only users with proper access permissions will be able to see this post.'),
            ),
            '#default_value' => $privacy,
            '#weight' => 99,
          );
        }
      }
      else {
        $form['private'] = array(
          '#type' => 'value',
          '#value' => $privacy,
        );
      }
    }
  }
}