Ok so a bit of background first on why I want to do this, this may also spark someone to suggest a better way of doing this also. I'm always open to ideas!

Basically I have a bunch of content types, call them rooms for the moment. I am using views to sort/search these rooms, works great (as expected with views).

Now what I want is to attach some php and an jquery script to each view result itself, this script will get an ID from a field attached to the view result, this will then be sent as an ajax request get some information and then come back with some results which will then be printed underneath the view result.

How do I go about doing this? I've taken a look at a few of the different hooks available for the views api but can't really figure out which one is the one I want.

Any suggestions are greatly appreciated. Dan


Try hook_views_pre_render() see docs and drupal_add_js().

  1. Add the correct JS using drupal_add_js by injecting the JS file using hook_views_pre_render() to fire on the correct view.
  2. Your JS will have to understand the default views output to re-theme / alter HTML output accordingly. Firebug or Chrome Developer tools can help you with that.
  3. you can use $view->attachment_after to attach like a custom blob of HTML like <div id="mycustom_content"></div> so you can muck with this div on your Ajax callback. You can add the view attachment in the view_pre_render

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.