6

OK so this is my hook form alter function.It is causing all the registration forms on site to be over written which I do not want as I just want it on this page.

 
function special_registration_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_register') {
    drupal_set_title(t('Custom registration'));
    $form['firstname'] = array('#type' => 'textfield', 
                               '#title' => t('First Name: *'), 
                               '#required' => TRUE, 
                               '#size' => 45, 
                               '#weight' => - 100,);
    $form['lastname'] = array('#type' => 'textfield', 
                              '#title' => t('Last Name: *'), 
                              '#required' => TRUE, 
                              '#size' => 45, 
                              '#weight' => - 99,);
  }
I only first name and last name to be captured and stored in a different table just on this page.

On other pages I just want the good old fashioned form. Do I still need to change the weight? I know I am missing something elementary.

Dan Heberden
  • 10,990
  • 3
  • 33
  • 29
user363036
  • 319
  • 1
  • 2
  • 7
  • Your arrays should _not_ have a comma at the end. '#weight' => - 99, should just end with 99. The comma is to put yet another key/value, which you do not have. Also, you'll need the closing brace on your function. – Dan Heberden Jun 14 '10 at 01:30
  • lol I know thanks!!! I didn't paste all the function. Thanks for taking a look at it. – user363036 Jun 14 '10 at 01:58
  • 1
    @Dan Heberden: The trailing comma after the last array entry is perfectly valid in PHP, and is adopted by many as 'normal', since it makes row wise code manipulations of array declarations much easier, provided the closing bracket is on a new line. Whether this is a good thing or not is an open debate not worth diving into ;) – Henrik Opel Jun 14 '10 at 09:47
  • @Henrik - Yeah, i was reviewing some standards stuff on zend and noticed that. I always thought that the trailing comma created a null element but after testing I see it has no bearing and makes adding elements in a row easier to not mess up. Thanks for pointing it out :) – Dan Heberden Jun 14 '10 at 17:01

5 Answers5

2

You just need a check for the current page, using either arg or $_GET['q'].

eg:

function special_registration_form_alter(&$form, $form_state, $form_id) {
if ($_GET['q'] !== 'whatever/path' ) { return false; }
..rest of code..
}
cam8001
  • 1,581
  • 1
  • 11
  • 22
  • Yes, $_GET['q'] is always the system path, so for example if your nodes are aliased to '/blog/2010/Jan/title-of-blog-post.html' $_GET['q'] will still be 'node/123'. – cam8001 Jun 15 '10 at 10:48
2

If you want to restrict your form alterations to a specific page, you can simply add a check for the page to your form id check, e.g.:

function special_registration_form_alter(&$form, $form_state, $form_id) {
  // Alter the registration form, but only on 'user/register' pages
  if ($form_id == 'user_register' && 'user' == arg(0) && 'register' == arg(1)) {
    // snipped alteration code
  }
}
Henrik Opel
  • 19,341
  • 1
  • 48
  • 64
  • Ok, I didn't notice cam8001s similar answer in time - still leaving this version, as it combines the page and form id checks (no need to return FALSE, btw) – Henrik Opel Jun 14 '10 at 10:03
  • If you are using Drupal 6, you can define your form alters like this: hook_form_FORM_ID_alter(&$form, &$form_state) , which some people find cleaner. http://api.drupal.org/api/function/hook_form_FORM_ID_alter/6 – cam8001 Jun 14 '10 at 16:02
0

You could make use of the Profile module in the Core list of modules as well. It will solve this without any programming, fyi.

Kevin
  • 13,153
  • 11
  • 60
  • 87
  • yes the more I talk to people, the more they advise me to use that or the content module. Guess I jumped the gun by diving into code. Thanks for the tip though :) – user363036 Jun 14 '10 at 01:59
0

Implement hook_user(); the function allow to alter the form presented to the users when they register on a site. hook_user() is used by user.module, and it is independent from the profile module.

Defining the hook as hook_user($op, &$edit, &$account, $category = NULL), the parameter $op will contain the value 'register' when the registration form is being presented to the user. In that case, the module returns the form fields it wants to add to the registration form.

apaderno
  • 28,547
  • 16
  • 75
  • 90
0

If you don't really need to create user accounts, like for a simple event registration. If instead you're collecting just names, you could use the webform module instead.

Oscar M.
  • 1,076
  • 7
  • 9