node_access_example_form_alter

5 node_access_example.module node_access_example_form_alter($form_id, &$form)
6 node_access_example.module node_access_example_form_alter(&$form, $form_state)
7 node_access_example.module node_access_example_form_alter(&$form, $form_state)
8 node_access_example.module node_access_example_form_alter(&$form, $form_state)

Implementation of hook_form_alter().

We use this to alter the node editing form and insert a check box so the admins can manage the node's access rights.

Modules may wish to provide default grants per node type using this hook.

File

developer/examples/node_access_example.module, line 72
This is an example illustrating how to restrict access to nodes based on some criterion associated with the user.

Code

function node_access_example_form_alter($form_id, &$form) {
  // This hook is called for all forms. We only want to work with node settings
  // and edit forms.
  if (isset($form['type'])) {
    // Node settings form.
    if ($form['type']['#value'] . '_node_settings' == $form_id) {
      // If the module needed it, this would be where you would insert controls
      // for the node's settings form.
    }

    // Node edit form for users with "administer nodes" permission.
    if ($form['type']['#value'] . '_node_form' == $form_id && user_access('administer nodes')) {
      $node = $form['#node'];
      if (!isset($node->access_example)) {
        // Load the grants from the database.
        $result = db_query('SELECT na.gid FROM {node_access} na WHERE na.nid = %d AND na.realm = \'example\' AND na.grant_view = 1', $node->nid);
        $grant = db_fetch_object($result);
        if ($grant && $grant->gid == 0) {
          // The "public" grant was set.
          $node->access_example = 0;
        }
        else {
          $node->access_example = 1;
        }
      }
      $form['access_example'] = array(
        '#type' => 'checkbox', 
        '#title' => t('Private Node Access'), 
        '#default_value' => $node->access_example, 
        '#weight' => -10, 
        '#description' => t('Make this node private.'),
      );
    }
  }
}
Login or register to post comments