[development] Adding to Profile forms - is hardcoding the only way?

Carl Mc Dade carl_mcdade at yahoo.com
Thu Aug 10 12:01:36 UTC 2006


Thanks! This looks like what I need to do. I will give your code a good study and try somethings.

- Carl

Alberto Lusiani <alusiani at gmail.com> wrote: Hello,

I may have an example that might be useful for what you are asking.
I am working on a module that facilitates using Drupal as a conference site.
I am using profile to define fields for registered participants, and I
use form_alter in my module in order to modify the profile form. I
include a code fragment:

/**
 * Implementation of hook_form_alter()
 *
 */
function tauconf_form_alter($form_id, &$form) {
  global $form_values;

  switch ($form_id) {

  // [...]
 
    $category = 'conference registration';
    if (isset($form[$category])) {

      //
      // change the /user/edit/conference+registration form
      // created with the profile module
      // - prepend the data from /user/edit/personal+information
      //

      //--- get user data
      $account = user_load(array('uid' => arg(1)));
      //--- get posted data if existing for profile_form_profile()
      $edit = $_POST['op'] ? $_POST['edit'] : (array)$account;

      //--- include "personal info" form
      $form[$category]['personal_information'] =
 profile_form_profile($edit, $account, "personal information");
      $form[$category]['personal_information']['#weight'] = -5;

      //
      // setup validation callback for 'conference registration' profile form
      //
      $form[$category]['#validate'] =
 array('tauconf_registration_validate' =>
       array(&$form_values, $account));

      //--- customize MAC address field
      $field = 'profile_mac_address';
      if (isset($form[$category][$field])) {
 $form[$category][$field]['#maxlength'] = 17;
 $form[$category][$field]['#size'] = 17;
      }


      //--- set default value of arriving date
      $field = 'profile_arrival_date';
      if (isset($form[$category][$field])) {
 $time = strtotime("2006-09-18 12:00");
 $form[$category][$field]['#default_value'] =
   array('day' => format_date($time, 'custom', 'j'),
  'month' => format_date($time, 'custom', 'n'),
  'year' => format_date($time, 'custom', 'Y'));
      }

      //--- set default value of departure date
      $field = 'profile_departure_date';
      if (isset($form[$category][$field])) {
 $time = strtotime("2006-09-22 16:00");
 $form[$category][$field]['#default_value'] =
   array('day' => format_date($time, 'custom', 'j'),
  'month' => format_date($time, 'custom', 'n'),
  'year' => format_date($time, 'custom', 'Y'));
      }

      //
      // prevent registration if:
      // - registrations are not open
      // - the e-mail is not invited
      //
      $field = 'profile_register_to_conference';
      if (isset($form[$category][$field])) {
 $form[$category][$field]['#weight'] = 5;
 $can_register = false;
 switch (variable_get('tauconf_reg_status', 'not_yet_open')) {
 case 'open':
   if (!array_search('invited', $account->roles)) {
     $msg = variable_get('tauconf_msg_not_invited','');
     $msg = str_replace('%user_name%', $account->name, $msg);
     $msg = str_replace('%user_mail%', $account->mail, $msg);
     $msg = str_replace('%contact_admin%',
         l(t('contact'), 'contact',
           array('title' =>
          t('Contact the site administrator'))),
         $msg);
   } else {
     $can_register = true;
   }
   break;
 case 'closed':
   $msg = variable_get('tauconf_msg_reg_closed', '');
   $can_register = false;
   break;
 case 'not_yet_open':
 default:
   $msg = variable_get('tauconf_msg_reg_not_yet_open', '');
   $can_register = false;
   break;
 }

 $can_unregister = array_search('registered', $account->roles);

 if (!$can_register) {
   //
   // replace checkbox for registration with explicative message
   //
   //--- replace null message with some info
   if ($msg == '') {
     $msg =
       '' .
       '' . "\n" .
       'Registration unavailable.' .
       '
' . "\n" . $msg . "\n" . '
';
   }
   //--- inform that registratoin is not available
   if ($can_unregister) {
     $msg =
       $msg .
       "If you unregister, you won't be able to register again.";
   }
   $form[$category]['msg'] = array(
       '#title' => t('Registration not available'),
       '#value' => $msg,
       '#weight' => 4,
       );
   if (!$can_unregister && !user_access('administer conference')) {
     //--- hide registration checkbox
     $form[$category][$field]['#type'] = 'hidden';
     $form[$category]['reg_status'] = array(
       '#title' => t('Registration status'),
       '#value' =>
       array_search('registered', $account->roles) ?
       "Status: registered" :
       "Status: not registered",
       '#weight' => 5,
       );
   }
 }
      }
    }
    break;
  }
}

Greetings,
-- 
Alberto



 Carl Mc Dade
____________________________
Web Developer
Dataföreningen i Sverige AB
www.d4d.se
www.dfs.se


 


 		
---------------------------------
Do you Yahoo!?
 Next-gen email? Have it all with the  all-new Yahoo! Mail Beta.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20060810/f90d2d1d/attachment.htm


More information about the development mailing list