I don't know if this is possible but could I do something like


  $foo = current_path(); 
  print $foo;


in page--foo.tpl, then something like

function MYMODULE_form_user_register_form_alter(&$form, &$form_state) {
    function name() {
        global $foo;
        if $foo == 'user/register/foo' {

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.

  • 2
    No you can't do that, templates are eval'd, and you lose any normal idea of global scope. The error is unrelated, that's because you've missed out the parenthesis for the conditional
    – Clive
    Jul 29, 2015 at 15:51
  • @Clive damn, is it possible to use a different user-register-form.tpl based on path?
    – delz
    Jul 29, 2015 at 16:10

1 Answer 1


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(&$form, &$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.

  • That works perfect thank you. What is target_role_id doing here exactly?
    – delz
    Jul 29, 2015 at 16:40
  • zhilevan, why did you remove the role check piece? Jul 30, 2015 at 22:34
  • @Delz you said in your question "I'm trying to make fields unique to roles without using profile2." If you know the role that you want (each has a numeric id) then the current user can be checked for it/them and the form can also be altered on that condition. Jul 30, 2015 at 22:37
  • that sounds like a good alternative to using auto assign role. I couldn't get it to work though. It would be ['account']['roles'][2]['#checked'] = TRUE; right?
    – delz
    Jul 31, 2015 at 14:47
  • What? No, users should not be able to choose their own role, that is a security related permission and you are asking to be hacked if you open that. Since they should not be able to choose, that field should not be accessible, so it won't bee in the form. And you are still thinking Javascript, it would be [#value] in the form or roles = array ( 2 => 2 ) in the user object, not [#checked]. Aug 2, 2015 at 2:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.