By zbb on
I have themed the login block in a way that it shows the login form when not logged in and when logged in it shows "Logged in as: " and a "Log out" link.
How do I make the login block show up even when a user is logged in? Can it be done without hacking core (or writing a whole new module)?
I know how to do it with hacking core and writing a module is also a solution. But WHY on earth does drupal hide the login block with some logic in core in the first place. AFAIR there is a way to show blocks only for anonymous users. So why is this hard coded instead?
(Yes, I know I can create a block and put php code in it. But that gets saved in the DB, and I'd rather have this in the file system.)
Comments
In the past I've done this by
In the past I've done this by making a custom function in my template.php file -- possibly a page preprocess function -- that contains the login form or logout link based on whether the user is logged in or not -- then printing it inside the page.tpl.php file.
I think you can just get away with recreating the form fields from the login block HTML (just be sure to include your form method, hidden inputs and etc.).
Let me know if you need help with how to write that.
Its hardcoded because the
Its hardcoded because the block by default has invalid links for people logged in so it makes sense to hide the block for people logged in. Most people just make a second block :).
I'm rolling with hacking core
I'm rolling with hacking core right now, because IMHO the "right" behavior is to always show the block. Its easy enough to get the old behavior back with making it visible only to anonymous users.
I'm a bit too lazy to write a module for my own login/logout block, so for now, I commented out two lines in core. Plus it's confusing for end users if there are to login blocks, and you have to tell them "look, you can't use that one because..."
Why would there be two login
Why would there be two login blocks? You can disable the default one on your blocks page. Then you could use the method I suggested to add a login form to the page template. It would do what you describe, without hacking core - and not putting PHP in the database, which you say you want to avoid.
Amanda
The block is still hanging
The block is still hanging around in the admin/build/block disabled section. Sooner or later a client enables that one, and not realizing it's the wrong one then calls me.
It's about reducing clutter for the not so technical minded. Drupal's usability is bad enough even with all these tricks.
Wait, after rereading the post, especially "...to add a login form to the page template." Yeah, that too works, except you can't move around the login form, since it's not a block.
For my personal use all these options are OK, but as soon as clients can get their hands on things... I have the feeling, Drupal isn't really client ready, yet. Especially when looking at translation (which I did for most of the day).
Maybe I'm just spoiled :)
Then it sounds like
Then it sounds like best-practice for your needs would be to use hook_form_alter in a custom module. Here's a good primer for altering forms from the module level for Drupal 6. Then you can take a look at the user_login_block code for the Forms API to see how to alter it. This is a bit out of my area of expertise, so I'd defer to those who are experienced with module development to take you further on that.
By client-ready, do you mean able to just hand over the site & have them manage it completely? If so, then no -- but CMS's that are that way are significantly less robust. That's a trade-off. I prefer to maintain relationships with clients so they aren't moving blocks around on their own, for example. That can lead to problems, for sure.
See also: Login block not
See also: Login block not shown on user/register.