Fix for Using hook_form_alter to fill in cck fields automatically and then hiding them from users

  1. function sitehelper_form_alter($form_id, &$form) {
  2.    global $user;
  3.    switch ($form_id) {
  4.  
  5.    // Only do this for the review type's form
  6.    case 'minfo_node_form':
  7.       // Get the parent node's id from the url (url created from a different node)
  8.       if(arg(1) == 'add') {
  9.          $pnid = $_GET['pnid'];
  10.          $newpnid = intval($pnid);
  11.          $pnode = node_load($newpnid);
  12.          // Set the parentnid field's value
  13.          $form['field_parentnid'][0]['value']['#value'] = $newpnid;
  14.       }
  15.      
  16.       if($user->uid != '1') {
  17.          // Hide the field
  18.          $form['field_parentnid'][0]['value']['#access'] = FALSE;
  19.       }
  20.    break;
  21.    }
  22. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.