node_access_example_form_alter

Versions
4.7 – 5
node_access_example_form_alter($form_id, &$form)
6
node_access_example_form_alter(&$form, $form_state)
7
node_access_example_form_alter(&$form, &$form_state, $form_id)

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.

Code

developer/examples/node_access_example.module, line 72

<?php
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
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.