hook_form_alter based on path I don't know if this is possible but could I do something like

&lt;?php 

  $foo = current_path(); 
  print $foo;

?&gt;


in page--foo.tpl, then something like

function MYMODULE_form_user_register_form_alter(&amp;$form, &amp;$form_state) {
    function name() {
        global $foo;
        if $foo == 'user/register/foo' {
            unset($form['account']['mail']);
    }
  }
}


in a custom module. I'm trying to make fields unique to roles without using profile2. Getting this error when I try it though:


  Parse error: syntax error, unexpected '$foo' (T_VARIABLE), expecting
  '(' in
  /var/www/html/drupal/sites/all/modules/custom_forms/custom_forms.module
  on line 71


Using Auto Assign Role for the path.
It looks like you have researched a few ideas and mixed them all.  The page--foo.tpl is a theme template file, and the names there are often used with theme suggestions so you can change the regions and wrapping html for different pages in different ways.  This is theme layer, and should be used for changing the appearance but not the processing.

The Hook_form_id_alter() is used to alter that specified form before it is passed to the theme layer, so that function will run before the print $foo; in the template.

So with what is in your question, the process would go like this:


  Check value of yet-to-be-set global $foo If foo is a specific page,
  remove field from form send form to theming get current page save
  current page to local $foo and ignore the global $foo that has already
  been checked. print out the value of local $foo into the html of the
  page


Also, I do not see roles touched anywhere in your question.  If I have read it right, you dont need the tpl at all and you can do this all in the form_alter.

function MYMODULE_form_user_register_form_alter(&amp;$form, &amp;$form_state) {
  global $user;
  if( current_path() == 'user/register/foo' ){
    // do what you want here
  }
}


Considerations.  Since you are using hook_form_ID_alter instead of hook_form_alter it is pretty safe to assume that this will only fire on the registration form so checking the registration url is a little redundant.  Since you are trying to define a function inside a function, you are used to javascript and the php scope handling is causing you some trouble; you should google for available drupal functions (like current_path() ) to help you before trying to write your own helper functions.