Adding to Profile forms - is hardcoding the only way?
I am adding a markup field to a Profile form but only want the field to show after a certain textarea. The idea is to use the links in the markup field to enter text in the textarea above it. I have set in the working code and have no problem with it but am questioning the fact that hardcoding this in seems to be the only way. Is there a way of doing this using the FormsAPI? a grouping syntax? Something like show this field only if a related field is present? Is there a hook for FormsAPI that would help? TIA Carl Mc Dade ____________________________ Web Developer for Hire http://www.heroforhire.net info@heroforhire.net --------------------------------- Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less.
take a look at how the filter forms work. There's help text in the node_example.module about it too. Essentially you need to create a grouping array, move the original element into it , and then put your new field into that. This is all perfectly possible with form_alter On 8/10/06, Carl Mc Dade <carl_mcdade@yahoo.com> wrote:
I am adding a markup field to a Profile form but only want the field to show after a certain textarea. The idea is to use the links in the markup field to enter text in the textarea above it. I have set in the working code and have no problem with it but am questioning the fact that hardcoding this in seems to be the only way.
Is there a way of doing this using the FormsAPI? a grouping syntax? Something like show this field only if a related field is present?
Is there a hook for FormsAPI that would help?
TIA
Carl Mc Dade ____________________________ Web Developer for Hire http://www.heroforhire.net info@heroforhire.net
------------------------------ Yahoo! Messenger with Voice. Make PC-to-Phone Calls<http://us.rd.yahoo.com/mail_us/taglines/postman1/*http://us.rd.yahoo.com/evt=39663/*http://voice.yahoo.com>to the US (and 30+ countries) for 2¢/min or less.
Hmm, After reading my own post I saw that I left out a critical point. The original fields are being created with profile.module. I have brought up the subject of changing this but for now I wanted to know if Forms API can hook into the dynamic creation of form fields handled by the profile.module Sorry about that. The previous post was badly formulated. I also thought of using straight markup to create a custom textarea with the HTML added but profile.module does not have this ability. Is anyone working on this type of functionality? - Carl Adrian Rossouw <adrian@bryght.com> wrote: take a look at how the filter forms work. There's help text in the node_example.module about it too. Essentially you need to create a grouping array, move the original element into it , and then put your new field into that. This is all perfectly possible with form_alter On 8/10/06, Carl Mc Dade <carl_mcdade@yahoo.com> wrote: I am adding a markup field to a Profile form but only want the field to show after a certain textarea. The idea is to use the links in the markup field to enter text in the textarea above it. I have set in the working code and have no problem with it but am questioning the fact that hardcoding this in seems to be the only way. Is there a way of doing this using the FormsAPI? a grouping syntax? Something like show this field only if a related field is present? Is there a hook for FormsAPI that would help? TIA Carl Mc Dade ____________________________ Web Developer for Hire http://www.heroforhire.net info@heroforhire.net --------------------------------- Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less. --------------------------------- Do you Yahoo!? Next-gen email? Have it all with the all-new Yahoo! Mail Beta.
crap! I hijacked the other thread again... I guess what I am looking for is a way to use another module to hook into profile.modules rendering of fields. So that I can change a particular field (textarea) using form_alter to change it to a group and add my markup field to that group. This way they can continue to use profile.module and I don't have to hardcode each instance. But I cannot see how to do this from the Form API docs. Carl Mc Dade <carl_mcdade@yahoo.com> wrote: Hmm, After reading my own post I saw that I left out a critical point. The original fields are being created with profile.module. I have brought up the subject of changing this but for now I wanted to know if Forms API can hook into the dynamic creation of form fields handled by the profile.module Sorry about that. The previous post was badly formulated. I also thought of using straight markup to create a custom textarea with the HTML added but profile.module does not have this ability. Is anyone working on this type of functionality? - Carl Adrian Rossouw <adrian@bryght.com> wrote: take a look at how the filter forms work. There's help text in the node_example.module about it too. Essentially you need to create a grouping array, move the original element into it , and then put your new field into that. This is all perfectly possible with form_alter On 8/10/06, Carl Mc Dade <carl_mcdade@yahoo.com> wrote: I am adding a markup field to a Profile form but only want the field to show after a certain textarea. The idea is to use the links in the markup field to enter text in the textarea above it. I have set in the working code and have no problem with it but am questioning the fact that hardcoding this in seems to be the only way. Is there a way of doing this using the FormsAPI? a grouping syntax? Something like show this field only if a related field is present? Is there a hook for FormsAPI that would help? TIA Carl Mc Dade ____________________________ Web Developer for Hire http://www.heroforhire.net info@heroforhire.net --------------------------------- Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less. --------------------------------- Do you Yahoo!? Next-gen email? Have it all with the all-new Yahoo! Mail Beta. Carl Mc Dade ____________________________ Web Developer Dataföreningen i Sverige AB www.d4d.se www.dfs.se --------------------------------- Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates starting at 1¢/min.
Op 10-aug-2006, om 12:13 heeft Carl Mc Dade het volgende geschreven:
crap! I hijacked the other thread again... Please stop hijacking threats and get yourself a decent mailclient, or read the messages before your push the 'Send' button.. This kind of behaviour screws up threat-follow-up in the more advanced mailclients like Mozilla's Thunderbird or Apples Mail as an example.
Stefan
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 = '<div class="form-item">' . '<div class="messages error">' . "\n" . '<label>Registration unavailable.</label>' . '</div>' . "\n" . $msg . "\n" . '</div>'; } //--- 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: <strong>registered</strong>" : "Status: <strong>not</strong> registered", '#weight' => 5, ); } } } } break; } } Greetings, -- Alberto
Thanks! This looks like what I need to do. I will give your code a good study and try somethings. - Carl Alberto Lusiani <alusiani@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.
Well I have tried and tried to use form_alter in every way I could think of and in ways that others have but the code is just ignored or goes to error; function profilecloud_form_alter($form_id, &$form) { $category = 'Medlemskonventet 2006'; if ($form_id == 'user_edit') { $alteration = array('#description' => t('ALTERED!!!!')); $form[$category]['edit-profile_matchmaking_2006'] = array_merge($form[$category]['edit-profile_matchmaking_2006'], $alteration); } } produces a array merge error. $form[$category]['edit-profile_matchmaking_2006'] is not recognized as an array and forcing it returns no changes. To get it to change this works. But the result is another complete form with a change within the original form. function profilecloud_form_alter($form_id, &$form) { $category = 'Medlemskonventet 2006'; if ($form_id == 'user_edit') { $alteration = array('#description' => t('ALTERED!!!!')); $form[$category]['edit-profile_matchmaking_2006'] = array_merge($form[$category], $alteration); } } Carl Mc Dade <carl_mcdade@yahoo.com> wrote: I am adding a markup field to a Profile form but only want the field to show after a certain textarea. The idea is to use the links in the markup field to enter text in the textarea above it. I have set in the working code and have no problem with it but am questioning the fact that hardcoding this in seems to be the only way. Is there a way of doing this using the FormsAPI? a grouping syntax? Something like show this field only if a related field is present? Is there a hook for FormsAPI that would help? TIA Carl Mc Dade ____________________________ Web Developer for Hire http://www.heroforhire.net info@heroforhire.net --------------------------------- Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2�/min or less. 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.
This was a touch better. still duplicated but not nested. function profilecloud_form_alter($form_id, &$form) { $category = 'Medlemskonventet 2006'; if ($form_id == 'user_edit') { $alteration = array('#description' => t('ALTERED!!!!')); //$form[$category]['edit-profile_matchmaking_2006'] = array_merge($form[$category],$alteration); $form['edit-profile_matchmaking_2006'] = array_merge($form[$category], $alteration); } } Carl Mc Dade <carl_mcdade@yahoo.com> wrote: I am adding a markup field to a Profile form but only want the field to show after a certain textarea. The idea is to use the links in the markup field to enter text in the textarea above it. I have set in the working code and have no problem with it but am questioning the fact that hardcoding this in seems to be the only way. Is there a way of doing this using the FormsAPI? a grouping syntax? Something like show this field only if a related field is present? Is there a hook for FormsAPI that would help? TIA Carl Mc Dade ____________________________ Web Developer for Hire http://www.heroforhire.net info@heroforhire.net --------------------------------- Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2�/min or less. Carl Mc Dade ____________________________ Web Developer Dataföreningen i Sverige AB www.d4d.se www.dfs.se --------------------------------- Get your email and more, right on the new Yahoo.com
Looks like this works but I have no idea why.. function profilecloud_form_alter($form_id, &$form) { $category = 'Medlemskonventet 2006'; if ($form_id == 'user_edit') { $alteration = array('#description' => t('ALTERED!!!!')); //$form[$category]['edit-profile_matchmaking_2006'] = array_merge($form[$category],$alteration); $form = array_merge($form[$category], $alteration); } } Carl Mc Dade <carl_mcdade@yahoo.com> wrote: I am adding a markup field to a Profile form but only want the field to show after a certain textarea. The idea is to use the links in the markup field to enter text in the textarea above it. I have set in the working code and have no problem with it but am questioning the fact that hardcoding this in seems to be the only way. Is there a way of doing this using the FormsAPI? a grouping syntax? Something like show this field only if a related field is present? Is there a hook for FormsAPI that would help? TIA Carl Mc Dade ____________________________ Web Developer for Hire http://www.heroforhire.net info@heroforhire.net --------------------------------- Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2�/min or less. Carl Mc Dade ____________________________ Web Developer Dataföreningen i Sverige AB www.d4d.se www.dfs.se --------------------------------- How low will we go? Check out Yahoo! Messengers low PC-to-Phone call rates.
<?php function profilecloud_form_alter($form_id, &$form) { $category = 'Medlemskonventet 2006'; if ($form_id == 'user_edit') { $alteration = array('#description' => t('ALTERED!!!!')); //$form[$category]['edit-profile_matchmaking_2006'] = array_merge($form[$category],$alteration); $form = array_merge($form[$category], $alteration); } } ?> Which produces results while nothing else in the docs or forum worked. The problem is while it does add text to the form it does not alter it. The description stays the same and the added text appears at the top of the form. The second problem is that the extra text seems to exist in its own array and stays at the top. Manipulating the array only moves the profile module fields and leaves the added text at the top. Everyone says use form _ alter but there is no documentation or examples of its use in the wild. The only way I have been able to get the exact results needed is to hardcode the profile module. Otherwise it does not seem possible. Is it? Carl Mc Dade <carl_mcdade@yahoo.com> wrote: I am adding a markup field to a Profile form but only want the field to show after a certain textarea. The idea is to use the links in the markup field to enter text in the textarea above it. I have set in the working code and have no problem with it but am questioning the fact that hardcoding this in seems to be the only way. Is there a way of doing this using the FormsAPI? a grouping syntax? Something like show this field only if a related field is present? Is there a hook for FormsAPI that would help? TIA Carl Mc Dade ____________________________ Web Developer for Hire http://www.heroforhire.net info@heroforhire.net --------------------------------- Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less. Carl Mc Dade ____________________________ Web Developer Dataföreningen i Sverige AB www.d4d.se www.dfs.se --------------------------------- Get your email and more, right on the new Yahoo.com
Hi Carl, sorry I could not followup earlier. I also had to debug my code until I found how it had to work, there are not so many form_alter examples available. I try to help you, however I am not a real expert. And I hope that the thread is acceptable in drupal development. On 8/10/06, Carl Mc Dade <carl_mcdade@yahoo.com> wrote:
Well I have tried and tried to use form_alter in every way I could think of and in ways that others have but the code is just ignored or goes to error;
function profilecloud_form_alter($form_id, &$form) { $category = 'Medlemskonventet 2006'; if ($form_id == 'user_edit') { $alteration = array('#description' => t('ALTERED!!!!')); $form[$category]['edit-profile_matchmaking_2006'] = array_merge($form[$category]['edit-profile_matchmaking_2006'], $alteration); } }
I added material to the form as follows by adding one array element, in your case it would be: $form[$category]['edit-profile_matchmaking_2006'] = $alteration; In setting up $alteration you can use '#weight' => <number> to position your addition. Note that profile does not set weights, you may have to set weights alco for the other array elements to reach what you want. Not using weight means (I think) the PHP ordering prevails, which (I think) means first assigned array members are first.
produces a array merge error. $form[$category]['edit-profile_matchmaking_2006'] is not recognized as an array and forcing it returns no changes.
To get it to change this works. But the result is another complete form with a change within the original form.
function profilecloud_form_alter($form_id, &$form) { $category = 'Medlemskonventet 2006'; if ($form_id == 'user_edit') { $alteration = array('#description' => t('ALTERED!!!!')); $form[$category]['edit-profile_matchmaking_2006'] = array_merge($form[$category], $alteration); [...]
The code asks for what you observe... On 8/11/06, Carl Mc Dade <carl_mcdade@yahoo.com> wrote:
<?php function profilecloud_form_alter($form_id, &$form) { $category = 'Medlemskonventet 2006'; if ($form_id == 'user_edit') { $alteration = array('#description' => t('ALTERED!!!!'));
//$form[$category]['edit-profile_matchmaking_2006'] = array_merge($form[$category],$alteration); $form = array_merge($form[$category], $alteration);
} } ?>
Which produces results while nothing else in the docs or forum worked. The
$form[$category]['Medlemskonventet 2006'] = $alteration; should also have worked. The firs array_merge statement in your code is ineffectual I think.
problem is while it does add text to the form it does not alter it. The description stays the same and the added text appears at the top of the form. The second problem is that the extra text seems to exist in its own
In order to decide where your addition is rendered you need to use weights (see above).
array and stays at the top. Manipulating the array only moves the profile module fields and leaves the added text at the top. Everyone says use form _ alter but there is no documentation or examples of its use in the wild. The only way I have been able to get the exact results needed is to hardcode the profile module. Otherwise it does not seem possible. Is it?
It is indeed possible to alter profile forms with form_alter, the code snippet I sent previously works for me (Drupal 4.7.x). Greetings, -- Alberto
On Fri, 11 Aug 2006 09:10:37 +0200, Carl Mc Dade <carl_mcdade@yahoo.com> wrote:
Which produces results while nothing else in the docs or forum worked. The problem is while it does add text to the form it does not alter it. The description stays the same and the added text appears at the top of the form. The second problem is that the extra text seems to exist in its own array and stays at the top. Manipulating the array only moves the profile module fields and leaves the added text at the top.
Everyone says use form _ alter but there is no documentation or examples of its use in the wild.
Drupal & modules being open source, there are a lot of examples (not all very good, mind you). Here's an example form_alter for the profile field named 'profile_firstname' in the category 'Personal' function scratch_form_alter($form_id, &$form) { if ($form_id == 'user_edit') { // Check whether we are at the right page if (isset($form['Personal'])) { // Modify $form['Personal']['profile_firstname']['#description'] = 'Descriptions are not check_plained!'; // Or unset, all works unset($form['Personal']['profile_firstname']); } } } If you do decide to file a bug report, please give full 'steps to reproduce' and what it is you are trying to do. Regards, Heine
participants (5)
-
Adrian Rossouw -
Alberto Lusiani -
Carl Mc Dade -
Heine Deelstra -
Stefan Nagtegaal