Execute Code Before View

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

OK, stupid question time. First a bit of background info...

I've used CCK to create a data type and a module to go with it. The module has only one function thus far and it's job is to query the DB and calculate a "volunteer score" reflecting how much volunteer work a member has logged in the 30 days prior to some target date. The whole purpose is so that I can do a better job of scheduling volunteers to work at the local non-profit theatre.

I've created a view with a URL of "roster" that does a nice job of reporting the information for me. I pass the target date into the view as an argument from the URL. Right now I'm calling my calculation function from PHP that I embedded into the "Header" text of the view. Probably not the right approach at all but it works...sort of.

The problem is that the view apparently assembles it's data before that header text is processed... I assume this because there's an option to hide the header text if the view returns empty. The result is I see the volunteer scores, but they are always from the previous instance of the view. For example, if I call for .../roster/today then .../roster/tomorrow, the "tomorrow" roster shows results for today. Only if I refresh the view do I get the results for "tomorrow".

So, is there a place or a way that I can easily call my function just before the view is populated? I tried making the view generate a block and placed the block into a page with my function call in the body of the page... but the argument handling is apparently lost there so it doesn't work.

Comments

Views for D6 or D5? Dan

dldege's picture

Views for D6 or D5?

Dan DeGeest
Lead Software Developer
iMed Studios
http://www.imedstudios.com/labs

Dan DeGeest
Software Developer
Somewhere or Another

Drupal 6

Summitt Dweller's picture

Sorry Dan... the view is currently broken as I'm actively playing with possibilities. The environment is Drupal 6.

Thanks for the reply and any suggestions you can make.

Mark

Mark

This might work for you -

dldege's picture

This might work for you - http://drupal.org/project/computed_field

Dan DeGeest
Lead Software Developer
iMed Studios
http://www.imedstudios.com/labs

Dan DeGeest
Software Developer
Somewhere or Another

I'll Give It a Shot

Summitt Dweller's picture

You know, I enabled that module yesterday and looked at it briefly but couldn't find much documentation (didn't look too hard either) so I dismissed it. However, now that I have my PHP code in hand the task of implementing a new computed field doesn't seem so daunting.

Thanks again for the suggestion Dan... I think you might be just what I need.

FYI... I'm on vacation next week traveling to San Antonio so don't be surprised or disappointed if I don't post a timely reply. However, I will post a reply if/when I get something working (or not).

Thanks. ...Mark

Mark

Found A Solution

Summitt Dweller's picture

Found a reference to this gem in a forum thread.... it works very nicely.

function hook_views_pre_execute( &$view )
{
if ( $view->name == 'roster' )
{
if ( $a2 = arg(2) )
calculate_volunteer_score( $a2 );
else
calculate_volunteer_score( );
}
}

Problem with computed_field was that I had to store the computed values in nodes before my view would see them so I was once again running into the timing issue. Thanks for getting me thinking again Dan.

Mark

Mark

If the timing is as you

dldege's picture

If the timing is as you suggests - which makes sense - there are probably several options for other places to hook in to run your code. Is this site/view accessible online to look at?

Dan DeGeest
Lead Software Developer
iMed Studios
http://www.imedstudios.com/labs

Dan DeGeest
Software Developer
Somewhere or Another

Central Iowa Drupal Users Group

Group categories

Category

Group notifications

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