3

I need help in moving over the User login form to the header of the same page in Drupal 7. I am new to Drupal and I am not very familiar with the Drupal hooks yet. How should I achieve this?

This is what I have tried so far.

template.php

function bklyn_theme() {
  $items = array();
  // create custom user-login.tpl.php
  $items['user_login'] = array(
  'render element' => 'form',
  'path' => drupal_get_path('theme', 'bklyn') . '/templates',
  'template' => 'user-login',
  'preprocess functions' => array(
  'bklyn_preprocess_user_login'
  ),
 );
 
 $items['user_pass'] = array(
 'render element' => 'form',
 'path' => drupal_get_path('theme', 'bklyn') . '/templates',
 'template' => 'user-pass',
 'preprocess functions' => array(
 'bklyn_preprocess_user_pass'
 ),
 );
 
return $items;
}

function bklyn_preprocess_user_login(&$vars) {
  $vars['intro_text'] = t('Username');
}

function bklyn_preprocess_user_pass(&$vars) {
  $vars['intro_text'] = t('Password');
}

I am not sure how I can integrate the login to show up in the header.

Before

What is now

After

How it should look and header region

2
  • Are you speaking of the user login form located in yourwebsite.com/user ? or are you referring to the user login block that shows up on most pages ?
    – No Sssweat
    Aug 31, 2016 at 17:44
  • from the website/user. I need the logon to place on the header content area and be able to style it. Aug 31, 2016 at 18:17

2 Answers 2

1

from the website/user. I need the logon to place on the header content area and be able to style it.

All you have to do is edit your theme's page.tpl.php

Since the form gets displayed as the page content:

in the header section of your template file add:

<?php if (current_path() == 'user'): ?>
     <?php print render($page['content']);?>
<?php endif; ?>

Then find the original <?php print render($page['content']); ?> and add:

  <?php if (current_path() != 'user'): ?>
     <?php print render($page['content']);?>
  <?php endif; ?>

OR... do the if (current_path() != 'user') { for everything inside the #main-wrapper

OR... use the if statement to add a div class that will colour your #main-wrapper background to red.

6
  • Thanks! I have a question about the if statements. The if statements are part of php so why I need to place <?php ?> for the print? Sep 1, 2016 at 13:25
  • It didn't work without the if statement in <?php and end if?>. I edited your code to reflect how it worked for me. Sep 1, 2016 at 13:36
  • Because I copy pasted and did not realize my syntax was wrong. If you <?php before the if and ?> after the closing } tag (from my original answer) it should work, as long as you don't have any HTML in between. The way you did it, is one way to add HTML code in between.
    – No Sssweat
    Sep 1, 2016 at 13:56
  • Your way, is the popular way for doing templates as its more visually friendly and allows you to modify and add HTML easily if you need to.
    – No Sssweat
    Sep 1, 2016 at 13:59
  • 1
    Yes right now I am styling to my requirements but either way your suggestion worked perfect. I appreciate your help! Sep 1, 2016 at 14:33
0

You don't need hooks to move the user login block to header. you can move the user login block to any region on the page. From block administer page move the login block to the header region. save and logout to check it working.

8
  • Well actually its not the block. It's from website.com/user Aug 31, 2016 at 18:18
  • Moving the block in the header region should do the job for you. Any specific reasons for moving a page inside a region??
    – Umed Godd
    Aug 31, 2016 at 18:22
  • I provided screenshots to better understand. Aug 31, 2016 at 18:29
  • But you can get same functionality by enabling login block in the header region.
    – Umed Godd
    Aug 31, 2016 at 18:33
  • I moved it but it does not show up and I cleared cache too. Aug 31, 2016 at 18:39

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.