I'd like to hack the sidebar output so that certain blocks aren't output if a user isn't logged in. I know that the path can be set to define when blocks are displayed based on the URL path ... But I want this type behavior based on logged in/not logged in.

Any ideas where I'd start hacking, and how hard this might be? I'd like to do this in a Theme independant fashion.


shane’s picture

Okay after a bit of investigation... Here's a thought on how to do this. Hacking the blocks.module is where I need to start. Basically I could add another Column to the block_admin_display() function similar to the way the "custom" column works. I'd call my column "hidden" (eg "hidden" from anonymous users). Then I'd need to add another row to the blocks table, called "hidden" with type of tinyint(2).

Insure that the table gets updated correctly if the "hidden" box is checked.

Then, modification of the block_list() function would be necessary to filter out which blocks are displayed. I believe I'd simply need to modify the SQL query:

SELECT * FROM {blocks} WHERE (status = '1' OR custom = '1')...


...WHERE (status = '1' OR custom = '1') AND hidden = '0'...

Which correlates to the hidden column being checked in the admin pages.

Does this sound reasonable?

pz’s picture

You could change your modules _block()-hook, check for correct permission (logged in) and if not correct return an empty array. But assumes your theme knows what to do with an empty block.

shane’s picture

Yep - thought of that, and it does work. But I wanted to do this in a theme-independent fashion. Thanks for the reply.

shane’s picture



For a patch to the 4.4.0 codebase. Your SQL table "blocks" will have to be altered to add the new row for support of this feature. Details in the patch/issue listed above.

tdailey’s picture

you can do this easily without hacking a module. Empty blocks won't display.

so, to show a block for not-logged in users that won't exist for logged-in users:

global $user;
if (!$user->uid){
return t($yourstring);

for logged in users, lose the !

global $user;
if ($user->uid){
return t($yourstring);
shane’s picture

Yep - I've used that successfully in the past for custom built blocks. However, blocks that are generated by the system (eg "Navigation", "Who's logged in", etc... or by other modules - events, forum, etc...) can't be controlled in this fashion.

My goal is to create a website that at first blush doesn't appear to be anything more than a typical "corporate" website. If an admin hits the "/user/login" page, logs in successfully - then suddenly it becomes a fully featured and rich Drupal site.

I'm also hacking the XTemplate theme to change the output of page/node/event (etc) information to NOT include things like "submitted by" etc... This way the data appears more cohesively like a real "corporate" site as opposed to a dynamic community oriented blog/forum/etc site.

Thanks for the info, though!