How do I overwrite SQL generated by a View?

Dev

How do I overwrite SQL generated by a View?

There are several reasons to overwrite an SQL query generated by a view whether it’s to improve performance or join to tables not available to the view.

After setting up the view, create a module with a function using hook_views_pre_execute.

 

function [module name]_views_pre_execute(&$view) {
  if($view->name==”[view name]”) {
   $sql = “[the new query here]”;
   $view->build_info[‘query’] = $sql;
  }
}

If you need to access arguments passed to the view look in $view->build_info[‘query_args’].

Requirements

If you don’t have Drupal set up yet, see this post by my friend Scott. He’ll walk you through the process (video).

More About the Author

Frank Suchomel

Web Developer
Display public facebook posts on your website A straightforward way to get your facebook public posts onto another website without requiring your guests to have a facebook account. ...
How do I overwrite SQL generated by a View? There are several reasons to overwrite an SQL query generated by a view whether it’s to improve performance or join to tables not ...

See more from this author →

Subscribe to our newsletter

  • I understand that InterWorks will use the data provided for the purpose of communication and the administration my request. InterWorks will never disclose or sell any personal data except where required to do so by law. Finally, I understand that future communications related topics and events may be sent from InterWorks, but I can opt-out at any time.
  • This field is for validation purposes and should be left unchanged.

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK

×

Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Germany
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072