How does Views query multiple taxonomy terms tied to content?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
wrburgess's picture

With Views, I already know how to render multiple taxonomy terms that are associated with a single piece of content. What I can't figure out is how Views actually queries the database to get the job done.

For example, this is easy to set up in Views:

Title: "Brett Favre is Hurt"
Body: "blah blah blah"
Tags: Football, Injury

Title: "Cliff Lee Signs Contract"
Body: "blah, blah, blah"
Tags: Baseball, Free Agency, Philadelphia

What I can't figure out is how Views goes about pulling the multiple tags and rendering them with each content item. The Views Preview query doesn't show any such SQL involving the taxonomy in the preview section (which I don't understand, either).

My guess is that a subquery of sorts is going on at the Taxonomy "field" or the query pulls multiple records and somehow groups the fields at title and body (which would seem rather inefficient).

I'm doing my best to follow Drupal standard practices for a custom module, and I'd like to know how the Drupal Views folks run these queries since they run pretty fast.

Comments

pre_render

merlinofchaos's picture

Every field has (as well as the style) has the option of doing additional things during pre_render(). In the Views tools menu, there is a setting that allows you to view all additional queries which will show you (amongst other things) what happens during the pre_render(). It will also show retrievals from cache if you're viewing filtered text, as well.

The general template is that pre_render() happens after the main query has run, so it knows all of the node IDs that the resultset pulled up. It then performs a single query against the taxonomy tables using the IDs that were in the resultset. It parses through the results and distributes them to the individual records, and then the render() method displays them in a list.

The actual code for this can be seen in views/modules/taxonomy/views_handler_field_term_node_tid.inc -- the pre_render runs the query and it populates $this->items. The render() method from the parent class knows to look in $this->items to figure out precisely what to render.

Thank you

wrburgess's picture

Thanks for the excellent reply!

Randy Burgess
rb@jbrb.com
773-733-0716
http://jbrb.com
Chicago, IL

Views Developers

Group organizers

Group notifications

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