How to invoke hook_block

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


19 points

Good morning,

I am building a module and invoking hook_block. I am wondering if there is a way for my block to gain some context about the page on which it will be rendered. Primarily I would like to know if my block can be aware of the URL of the page being displayed (not the http-requested URL in case they differ), and the access permissions of the page being displayed (not those of the block itself, or of the user accessing the block.

Thanks,
Everett Zufelt
http://zufelt.ca



-3 points

$_GET['q'] holds the current, internal system path.

menu_get_item() provides you the currently active menu router item.

menu_get_object() provides you a dynamic argument context from the menu router item, if any.
http://api.drupal.org/api/function/menu_get_object/6

sun

Anonymous's picture
Created by Anonymous
10 points

Thanks, this was quite helpful.

I notice that menu_get_item() will tell me if the current user can access the current menu item. Is there a simple method to test if user0 can access the current menu item? That is, regardless who the current user is, I would like to see if there is a function to let me know if user0 can access the current page, essentially a test to see if the current page is available to anonymous users or not.

Thanks again,
Everett Zufelt
http://zufelt.ca

Anonymous's picture
Created by Anonymous
0 points

I don't think there's an easy way. The access checking for
menu_get_item() is done in _menu_check_access(). This figures out and calls the access callback for the particular menu item. For the most general case of a menu item with a custom access callback, it would probably not be possible to modify the function to check a specific
$account instead of the current global $user, without some sort of hack.

-Jennifer
--
Jennifer Hodgdon * Poplar ProductivityWare
www.poplarware.com
Drupal, WordPress, and custom Web programming

Anonymous's picture
Created by Anonymous
-5 points

Would it be acceptable in the Drupal community for me to solve this problem by creating a user0 object, switching it with the global $user, perform the test, and then switch back? By acceptable I mean are there any significant problems I shoud be aware of if using this approach?

Thanks,
Everett Zufelt

Anonymous's picture
Created by Anonymous
-1 points

That's the "hack" I was referring to. As far as acceptability goes, I don't know what to tell you. If it's in your own private module, no one can complain...

--Jennifer

Anonymous's picture
Created by Anonymous
-7 points

I am planning on releasing as a contrib module, so if there is a better way to test to see if a menu item is available for anonymous access without a hack I'd happily implement it.

Thanks,
Everett Zufelt
http://zufelt.ca

Anonymous's picture
Created by Anonymous
3 points

user_access takes an optional account parameter.
I think
$account = user_load(0);
user_access('perm', $account);
Should work...

~Sam

Anonymous's picture
Created by Anonymous
3 points

Thanks for this.

I took a look at user_access() but wasn't sure that it would do what I needed. I have to admit to being a newbie as far as Drupal development goes. I normally work on cor accessibility.

As far as user_access() I see that it can accept a user object, but that it also needs me to pass a permission to check. What permission would I be checking to see if the current system path can be accessed? This is why menu_get_item() seemed more appropriate. It would be nice if there was a function like menu_get_item() that accepted the user object like user_access() does. Perhaps I'll ad an issue as a feature request for d8 if this doesn't already exist in some hidden corner of d6.

Thanks,
Everett Zufelt
http://zufelt.ca

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.