Thursday, 17 February 2011

Form alter help on drupal

To implement hook_form_alter, simply create a function called 
modulename_form_alter, using the parameters shown. Drupal will 
automatically use this function if it exists in your module.
 
function mysite_form_alter(&$form, &$form_state, $form_id) {
  // print $form_id;
  switch ($form_id) {
    case 'blog_node_form':
      // remove some unwanted node editing fields
      $form['revision_information']['#access'] = 0;
      $form['menu']['#access'] = 0;
      $form['author']['#access'] = 0;
      // override node options permissions defined by 'administer nodes'
      $form['options']['#access'] = 1;
      $form['options']['status']['#access'] = 1;
      $form['options']['promote']['#access'] = 0;
      $form['options']['sticky']['#access'] = 0;
    break;
    case 'page_node_form':
      // don't ever allow page nodes to be promoted or sticky
      $form['options']['promote']['#access'] = 0;
      $form['options']['sticky']['#access'] = 0;
    break;
  }
}
 
 //
$form['field_teacher']['#default_value']= Array (0 => Array ( 'uid' => 15));
 
$form['field_teacher'][0]['#default_value']['uid']= $form['uid']['#value'];
1) hook_form_FORM_ID_alter()

2) hook_form_alter()

3) hook_form_FORM_ID_post_alter()
 
if($form_id='author_node_form') {
        $form['author']=NULL;
        $form['taxonomy']=NULL;
        $form['options']=NULL;
        $form['menu']=NULL;
        $form['comment_settings']=NULL;
        $form['files']=NULL;
        $form['revision_information']=NULL;
        $form['attachments']=NULL;
        if($form["HIDEPREVIEW"]) {
          $form['buttons']['preview']=NULL;
          $form['buttons']['delete']=NULL;
        }
 
 ****
<?phpfunction genpass_form_alter(&$form, $form_state, $form_id) {
 
  switch(
$form_id) {
 
    case
'user_admin_settings':
   
     
$form['registration']['genpass_mode'] = array(
       
'#type' => 'radios',
       .....
     
$form['#validate'][] = 'genpass_user_admin_settings_validate';
     
$form['#submit'][] = 'genpass_user_admin_settings_submit';
      break;
     
    case
'myaddaleader_form':
    case
'myaddamember_form':
    case
'user_register':
     
$pass_item =& genpass_get_pass_item($form);
     
// alter the user-admin register (no mode decision)
     
if ($form['destination']['#value'] == 'admin/user/user/create' && user_access('administer users')) {
       
$pass_item['#required'] = FALSE;
       ....  
?>
mymodule (yes, I called it mymodule)
<?phpfunction mymodule_form_alter (&$form, &$form_state, $form_id) {

  switch (
$form_id) {
    case
'group_node_form':
     
//    Set the value to what is desired
     
$form['og_description']['#default_value'] = "a group";
     
// Hide the form item, leave this at the above value
     
$form['og_description']['#access'] = 0;
      break;
    case
'user_register':
     
// use dpm to see the form array
     
return dpm($form);
      break;
    case
'myaddamember_form':// this next bit is cut and paste from content profile module
     
require_once drupal_get_path('module', 'node') .'/node.pages.inc';

     
// Allow other modules to customize the used profile types, so modules
      // can easily customize the registration form.
     
$default_types = content_profile_get_types('names', (arg(0) == 'admin' ? 'admin_user_create_use' : 'registration_use'));
     
$form += array('#content_profile_registration_use_types' => $default_types);

      foreach (
$form['#content_profile_registration_use_types'] as $type => $typename) {
       
content_profile_registration_add_profile_form($type, $form, $form_state);
      }
// end the bit cut and paste from the content profile module

     
genpass_form_alter($form, $form_state, $form_id);

     
$form['account']['notify']['#default_value'] = TRUE;

     
$form['account']['pass']['#access'] = FALSE;
     
$form['account']['status']['#access'] = FALSE;
     
$form['revision_information']['#access'] = FALSE;
     
$form['comment_settings']['#access'] = FALSE;
     
$form['locale']['#access'] = FALSE;
     
$form['path']['#access'] = FALSE;
     
$form['print']['#access'] = FALSE;
     
$form['book']['#access'] = FALSE;
     
$form['menu']['#access'] = FALSE;
     
$form['group_phone']['field_phone_privacy']['#access'] = FALSE;
     
$form['group_email']['#access'] = FALSE;
     
$form['group_homeaddress']['field_homeaddress_privacy']['#access'] = FALSE;
     
$form['group_homeaddress']['field_homeaddress_public']['#access'] = FALSE;
     
$form['group_homeaddress']['field_homeaddress_leaderviewable']['#access'] = FALSE;
     
$form['group_homeaddress']['field_homeaddress_adminview']['#access'] = FALSE;
      break;
    case
'myaddaleader_form':// this next bit is cut and paste from content profile module
     
require_once drupal_get_path('module', 'node') .'/node.pages.inc';

     
// Allow other modules to customize the used profile types, so modules
      // can easily customize the registration form.
     
$default_types = content_profile_get_types('names', (arg(0) == 'admin' ? 'admin_user_create_use' : 'registration_use'));
     
$form += array('#content_profile_registration_use_types' => $default_types);

      foreach (
$form['#content_profile_registration_use_types'] as $type => $typename) {
       
content_profile_registration_add_profile_form($type, $form, $form_state);
      }
// end the bit cut and paste from the content profile module

     
genpass_form_alter($form, $form_state, $form_id);

     
$form['account']['notify']['#default_value'] = TRUE;

     
$form['account']['pass']['#access'] = FALSE;
     
$form['account']['status']['#access'] = FALSE;
     
$form['revision_information']['#access'] = FALSE;
     
$form['comment_settings']['#access'] = FALSE;
     
$form['locale']['#access'] = FALSE;
     
$form['path']['#access'] = FALSE;
     
$form['print']['#access'] = FALSE;
     
$form['book']['#access'] = FALSE;
     
$form['menu']['#access'] = FALSE;
     
$form['group_homeaddress']['field_homeaddress_public']['#access'] = FALSE;
     
$form['group_homeaddress']['field_homeaddress_leaderviewable']['#access'] = FALSE;
     
$form['group_homeaddress']['field_homeaddress_adminview']['#access'] = FALSE;

     
// use dpm to see the form array
     
return dpm($form);
      break;
  }
}

function
myaddamember_form($form_state)
{
 
$form=array();
 
// Merge in the default user edit fields.
 
$form = array_merge($form, user_register($form_state, NULL, NULL, TRUE));
// add this role as default, already checked the checkbox
 
$form['account']['roles']['4'] = array(
   
'#type' => checkbox,
   
'#title' => 'Member',
   
'#default_value' => TRUE,
  );
// remove a bunch of roles by setting the allowed options to just these
 
$form['account']['roles']['#options']= array(
   
'4' => 'Member',
   
'5' => 'Leader Applicant',
  );
// make submitting the form actually create a user
 
$form['#submit'][] = 'user_register_submit';

  return
$form;
}

function
myaddaleader_form($form_state)
{
 
$myoptions = array('1' => t('Enabled'), '0' => t('Disabled'));
 
$form=array();
/*
// test helloworld type form building. keeping during testing to remind me how to do this.
  $form['addaleader'] = array(
    '#type' => 'fieldset',
    '#title' => t('Leader account settings'),
    '#tree' => TRUE,
  );
  $form['addaleader']['thefirstoption'] = array(
    '#type' => 'radios',
    '#title' => t('First Option'),
    '#default_value' => variable_get('thefirstoption', 0),
    '#options' => $myoptions,
    '#description' => t('The First Option.'),
  );
// end test helloworld type form building
*/

  // Merge in the default user edit fields.
 
$form = array_merge($form, user_register($form_state, NULL, NULL, TRUE));

 
// check some roles by default
 
$form['account']['roles']['3'] = array(
   
'#type' => checkbox,
   
'#title' => 'Leader',
   
'#default_value' => TRUE,
  );

 
$form['account']['roles']['4'] = array(
   
'#type' => checkbox,
   
'#title' => 'Member',
   
'#default_value' => TRUE,
  );
// keeping to remind me how
//  $form['hidden'] = array('#type' => 'value', '#value' => 'is_it_here');
//  $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));

// make submitting the form actually create a user
//  $form['#validate'][] = 'user_register_validate';
 
$form['#submit'][] = 'user_register_submit';

  return
$form;
}

function
mymodule_menu() {
 
$items = array();
// make two new tabs, showing the new create user forms, parallel to the system default add user form
 
$items['admin/user/user/createusermember'] = array(
   
'title' => 'Add user (a Member)',
   
'description' => 'Create a user account for a Member.',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('myaddamember_form'),
   
'access arguments' => array('administer users'),
   
'type' => MENU_LOCAL_TASK,
  );

 
$items['admin/user/user/createuserleader'] = array(
   
'title' => 'Add user (a Leader)',
   
'description' => 'Create a user account for a Leader.',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('myaddaleader_form'),
   
'access arguments' => array('administer users'),
   
'type' => MENU_LOCAL_TASK,
  );

  return
$items;
}
?>
MYMODULE_node_form_submit() is not documented so why this code invoke it?

+  // This form uses button-level submit handlers only, which manually invoke
+  // node_form_submit_build_node(), which in turn executes any form-level submit
+  // handlers prior to invoking node_submit(). For programmatically defined node
+  // types, a TYPE_node_form_submit() handler is therefore expected to be
+  // invoked, if it exists.
+  $form['#validate'][] = 'node_form_validate';
+  $form['#submit'] = array();
+  if (function_exists($node->type . '_node_form_submit')) {
+    $form['#submit'][] = $node->type . '_node_form_submit';
+  }
 
    

No comments:

Post a Comment

Source base installation of php, mysql and apache in ubuntu/ linux

Compile and Install a LAMP(Linux/Apache/MySQL/PHP) Server from Source In the last post, I described the method to install a LAMP ser...