Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By shane on
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.
Comments
Some thoughts on how to do this
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')...
To
...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?
_block-hook
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.
yep - thanks
Yep - thought of that, and it does work. But I wanted to do this in a theme-independent fashion. Thanks for the reply.
Patch created - functionality added
See:
http://drupal.org/node/view/6254
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.
you can do this easily withou
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:
for logged in users, lose the !
But that doesn't work for "system" blocks
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!