0

I have two separate login pages for users and administrators: /users/login and backend/login.

I have created a new function in my custom theme.

  function customtheme_theme() {
    $items = array(); 
    $items['user_login'] = array(
      'render element' => 'form',
      'path' => drupal_get_path('theme', 'customtheme') . '/templates/user',
      'template' => 'user-login',
    );

    $items['user_pass'] = array(
      'render element' => 'form',
      'path' => drupal_get_path('theme', 'customtheme') . '/templates/user',
      'template' => 'user-pass',
    );

    return $items;
  }

In /sites/all/themes/customtheme/templates/user, I created 2 files, user-login.tpl.php and user-pass.tpl.php. In user-login.tpl.php, I put the following code.

<?php $login_form = drupal_get_form("user_login"); ?>

<?php print $messages; ?>

<input autocomplete="off" type="text" 
value="<?php echo $login_form['name']['#value']; ?>" 
id="<?php echo $login_form['name']['#id']; ?>" 
name="<?php echo $login_form['name']['#name']; ?>" 
placeholder="Enter your username" required="" 
oninvalid="this.setCustomValidity('Username id is required')" 
oninput="setCustomValidity('')"/>

// password field..
// actions ..

So, I used the same setup for the admin login as well.

I created a new template at /sites/all/themes/customtheme/templates/page/page--backend.tpl.php. In that page, I just rendered the user_login form.

$elements = drupal_get_form("user_login"); 
$form = drupal_render($elements);   
echo ($form);

Now, both pages are working fine, but I need to show some message saying you are not allowed here if normal users logged in through the backend form.

I am not able to figure out at which place/file/path I need to check for the user role and display the message (push to $messages).

  • The only way to add a validation or submission handler to a form is in a module. You cannot do that in a template, and it would be wrong to do that in a template. It is also wrong to manually create a form in a template. For that, there is the form API. – kiamlaluno Aug 6 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.