Blocks and page context
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 Follow me on Twitter http://twitter.com/ezufelt View my LinkedIn Profile http://www.linkedin.com/in/ezufelt
$_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 ________________________________ From: development-bounces@drupal.org [mailto:development-bounces@drupal.org] On Behalf Of E.J. Zufelt Sent: Friday, April 23, 2010 4:49 PM To: development@drupal.org Subject: [development] Blocks and page context 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 Follow me on Twitter http://twitter.com/ezufelt View my LinkedIn Profile http://www.linkedin.com/in/ezufelt
Also, arg(0), arg(1), arg(2), ... are available to the block. These are the real path components, not the URL alias stuff. Nancy E. Wichmann, PMP Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr. ________________________________ From: Daniel F. Kudwien <news@unleashedmind.com> To: development@drupal.org Sent: Fri, April 23, 2010 12:03:17 PM Subject: Re: [development] Blocks and page context $_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 ________________________________ From: development-bounces@drupal.org [mailto:development-bounces@drupal.org] On Behalf Of E.J. Zufelt Sent: Friday, April 23, 2010 4:49 PM To: development@drupal.org Subject: [development] Blocks and page context 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 Follow me on Twitter http://twitter.com/ezufelt View my LinkedIn Profile http://www.linkedin.com/in/ezufelt
Good afternoon, 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 Follow me on Twitter http://twitter.com/ezufelt View my LinkedIn Profile http://www.linkedin.com/in/ezufelt On 2010-04-23, at 12:03 PM, Daniel F. Kudwien wrote:
$_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
________________________________
From: development-bounces@drupal.org [mailto:development-bounces@drupal.org] On Behalf Of E.J. Zufelt Sent: Friday, April 23, 2010 4:49 PM To: development@drupal.org Subject: [development] Blocks and page context
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
Follow me on Twitter http://twitter.com/ezufelt
View my LinkedIn Profile http://www.linkedin.com/in/ezufelt
E.J. Zufelt wrote:
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.
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
Good afternoon, Thanks for the response. 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 again, Everett Zufelt http://zufelt.ca Follow me on Twitter http://twitter.com/ezufelt View my LinkedIn Profile http://www.linkedin.com/in/ezufelt On 2010-04-23, at 6:12 PM, Jennifer Hodgdon wrote:
E.J. Zufelt wrote:
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.
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
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 E.J. Zufelt wrote:
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?
On 2010-04-23, at 6:12 PM, Jennifer Hodgdon wrote:
E.J. Zufelt wrote:
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. 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
Good afternoon, 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 Follow me on Twitter http://twitter.com/ezufelt View my LinkedIn Profile http://www.linkedin.com/in/ezufelt On 2010-04-23, at 6:58 PM, Jennifer Hodgdon wrote:
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
E.J. Zufelt wrote:
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?
On 2010-04-23, at 6:12 PM, Jennifer Hodgdon wrote:
E.J. Zufelt wrote:
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. 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
Is this in a test? Then you can probably use $this->userLogout() and then browse, I think? Jennifer E.J. Zufelt wrote:
Good afternoon,
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.
On 2010-04-23, at 6:58 PM, Jennifer Hodgdon wrote:
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
E.J. Zufelt wrote:
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? On 2010-04-23, at 6:12 PM, Jennifer Hodgdon wrote:
E.J. Zufelt wrote:
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. 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
user_access takes an optional account parameter. I think $account = user_load(0); user_access('perm', $account); Should work... "Jennifer Hodgdon" <yahgrp@poplarware.com> wrote:
Is this in a test? Then you can probably use $this->userLogout() and then browse, I think?
Jennifer
E.J. Zufelt wrote:
Good afternoon,
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.
On 2010-04-23, at 6:58 PM, Jennifer Hodgdon wrote:
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
E.J. Zufelt wrote:
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? On 2010-04-23, at 6:12 PM, Jennifer Hodgdon wrote:
E.J. Zufelt wrote:
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. 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
-- Sent from my Android phone with K-9 Mail. Please excuse my brevity.
Good evening, 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 Follow me on Twitter http://twitter.com/ezufelt View my LinkedIn Profile http://www.linkedin.com/in/ezufelt On 2010-04-23, at 7:23 PM, Sam Tresler wrote:
user_access takes an optional account parameter.
I think
$account = user_load(0); user_access('perm', $account);
Should work...
"Jennifer Hodgdon" <yahgrp@poplarware.com> wrote:
Is this in a test? Then you can probably use $this->userLogout() and then browse, I think?
Jennifer
E.J. Zufelt wrote:
Good afternoon,
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.
On 2010-04-23, at 6:58 PM, Jennifer Hodgdon wrote:
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
E.J. Zufelt wrote:
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? On 2010-04-23, at 6:12 PM, Jennifer Hodgdon wrote:
E.J. Zufelt wrote: > 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. 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
-- Sent from my Android phone with K-9 Mail. Please excuse my brevity.
E.J. Zufelt wrote:
Good evening,
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.
Context is a major weakness in Drupal right now and there is currently a lot of discussion on what to do about that in D8. As for what you can do right now, your global $user hack is probably the only thing you can actually do. it's not elegant at all, but in a contrib module...be very very careful you don't change the $user identity once you're done (so make sure all possible failure scenarios are covered).
Good evening Earl, Thanks for the comment. I really enjoyed the context and content discussion on Saturday at the Core Developer Summit. Hoping to see a drupal_butler object in drupal 8. Are any discussions about this happening online, if so, where can I get involved and contribute? Thanks again, Everett Zufelt http://zufelt.ca Follow me on Twitter http://twitter.com/ezufelt View my LinkedIn Profile http://www.linkedin.com/in/ezufelt On 2010-04-23, at 8:27 PM, Earl Miles wrote:
E.J. Zufelt wrote:
Good evening,
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.
Context is a major weakness in Drupal right now and there is currently a lot of discussion on what to do about that in D8. As for what you can do right now, your global $user hack is probably the only thing you can actually do. it's not elegant at all, but in a contrib module...be very very careful you don't change the $user identity once you're done (so make sure all possible failure scenarios are covered).
E.J. Zufelt wrote:
Good evening Earl,
Thanks for the comment.
I really enjoyed the context and content discussion on Saturday at the Core Developer Summit. Hoping to see a drupal_butler object in drupal 8. Are any discussions about this happening online, if so, where can I get involved and contribute?
Hi Everett, If you do go down the path of changing the global $user object, then you will want to use session_save_session to make your code look something like this: // Don't save the session if the next block of code fails: session_save_session(FALSE); // Save the old user account someplace. $user = user_load(0); // Some computation.... // Something like this: $user = $the_old_user // Its now safe to start saving the session again: session_save_session(TRUE); </code> If your 'Computation' in the middle there fails, then the users session will be saved with the $user object you loaded up, i.e. they'll get logged out. Annoying at the moment, but if you ever change it to assign other users to the $user object then you might end up with people being logged in as other people if the 'computation' fails for some reason. Regards Steven Jones ComputerMinds ltd - Perfect Drupal Websites Phone : 024 7666 7277 Mobile : 07702 131 576 Twitter : darthsteven http://www.computerminds.co.uk On 23 April 2010 23:17, E.J. Zufelt <lists@zufelt.ca> wrote:
Good afternoon, Thanks for the response. 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 again, Everett Zufelt http://zufelt.ca Follow me on Twitter http://twitter.com/ezufelt
View my LinkedIn Profile http://www.linkedin.com/in/ezufelt
On 2010-04-23, at 6:12 PM, Jennifer Hodgdon wrote:
E.J. Zufelt wrote:
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.
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
Everett, I think if the currently logged in user does not have access to this URL, then the page load will not reach as far as your hook_block() code. Although, it is 10:31 pm and I am at the airport after 7 continuous days of Drupaling, so brain is fried (in the microwave). So better test it with some test code and see if what I said is right or not. On Fri, Apr 23, 2010 at 5:30 PM, E.J. Zufelt <lists@zufelt.ca> wrote:
Good afternoon,
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
Follow me on Twitter http://twitter.com/ezufelt
View my LinkedIn Profile http://www.linkedin.com/in/ezufelt
On 2010-04-23, at 12:03 PM, Daniel F. Kudwien wrote:
$_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
________________________________
From: development-bounces@drupal.org [mailto:development-bounces@drupal.org] On Behalf Of E.J. Zufelt Sent: Friday, April 23, 2010 4:49 PM To: development@drupal.org Subject: [development] Blocks and page context
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
Follow me on Twitter http://twitter.com/ezufelt
View my LinkedIn Profile http://www.linkedin.com/in/ezufelt
-- Khalid M. Baheyeldin 2bits.com, Inc. http://2bits.com Drupal optimization, development, customization and consulting. Simplicity is prerequisite for reliability. -- Edsger W.Dijkstra Simplicity is the ultimate sophistication. -- Leonardo da Vinci
participants (8)
-
Daniel F. Kudwien -
E.J. Zufelt -
Earl Miles -
Jennifer Hodgdon -
Khalid Baheyeldin -
nan wich -
Sam Tresler -
Steven Jones