Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override form just on one page?

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.

like image 970
user363036 Avatar asked Jun 14 '10 01:06

user363036


2 Answers

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..
}
like image 193
cam8001 Avatar answered Oct 14 '22 13:10

cam8001


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
  }
}
like image 27
Henrik Opel Avatar answered Oct 14 '22 14:10

Henrik Opel