Programmatic Custom Access

Events happening in the community are now at Drupal community events on www.drupal.org.
Summitt Dweller's picture

Trying to solve what would appear to be a simple problem, but frankly Drupal's handling of access/permissions/grants has my head spinning.

On one of my sites I have a View that displays a list of phone numbers submitted (via user profile) by users who agree to share them with other users who opt into the same list. The View is working nicely so now the only trick is to elegantly grant view access only to those users who have entered their own phone number into the system. Users who opt NOT to share their phone number need to be excluded from seeing the View/list.

I'm looking for the quick and easy way to do this (programatically if necessary) without having to create a "special" role dedicated just to this task.

Suggestions? As always, any advice you can offer is greatly appreciated. Thanks.

Comments

Hook_menu_alter ??

Michael Hofmockel's picture

http://api.drupal.org/api/function/hook_menu_alter

If you altered the menu item that is created by the view (assuming a page) then you could run your own access callback.

function yourmodule_menu_alter(&$items) {
  $items['path/to/your/view']['access callback'] = 'yourmodule_sharedphone';
}

function yourmodule_sharedphone() {
  if (User shared) return TRUE;
  return FALSE;
}

clear the cache under admin/settings/performance to rebuild the menu system when you install this.

Regards,
Michael Hofmockel

Open Source || Open Access || Open Mind

Thanks

Summitt Dweller's picture

I wasn't thinking along those lines at all but that does sound like a relatively simple approach. I'll take a shot at it as soon as I have time... and I'll let you know how it comes out.

Thanks for the suggestion!

Mark

Here is a suggestion....

cgapperi's picture

Not sure I like the concept of altering view module to access hook_access but....

http://www.johnandcailin.com/blog/cailin/advanced-drupal-views-access-co...

Done!

Summitt Dweller's picture

Thought I would post a follow-up now that I have this issue resolved, at least temporarily...

I tried Michael's suggestion and had a long look at the post referenced by cgapperi (thank you both!). Could not get hook_menu_alter to work...my hook never seemed to get called?

Anyway, after a bit more searching I found the Views Hacks (views_hacks) module. It includes a views plug-in that provides "PHP Code" as an addition to Views' access restriction option set (as well as 6-7 other handy Views features). I put it to use with my view and a call to a custom igo_paf_access( ) function which queries the DB and returns the status I'm looking for in this case.

This technique works nicely EXCEPT that my query gets called 3 times when I open the page that contains my view. I don't mind it being called that many times but I really don't want to be querying the database every time it does so. Unfortunately, my debugger is broken (no clue what happened there) so I can't easily see what's available to me inside this function to get a better control. I can only imagine that the page fetch involves separate READ, VALIDATE and DISPLAY operations (or something like that), but I have no way of getting to the details of those operations. Anybody have an idea how to ferret this out and get it under control?

Thanks again guys. Take care.

Mark

Central Iowa Drupal Users Group

Group categories

Category

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week