How to override form just on one page?
Asked Answered
V

5

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.

Validate answered 14/6, 2010 at 1:22 Comment(4)
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.Mankind
lol I know thanks!!! I didn't paste all the function. Thanks for taking a look at it.Validate
@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 ;)Petite
@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 :)Mankind
H
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..
}
Heraldic answered 14/6, 2010 at 9:47 Comment(1)
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'.Heraldic
P
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
  }
}
Petite answered 14/6, 2010 at 9:52 Comment(2)
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)Petite
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. api.drupal.org/api/function/hook_form_FORM_ID_alter/6Heraldic
P
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.

Piselli answered 14/6, 2010 at 1:31 Comment(1)
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 :)Validate
A
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.

Antiicer answered 15/6, 2010 at 19:27 Comment(0)
S
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.

Snub answered 15/7, 2010 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.