How to override Drupal views request?

How to replace Views 3 query with custom SQL query in code?

+4
source share
3 answers

From what I understand, you can use hook_views_query_alter to modify the query. I would suggest that you can also use it to replace a query. Here are some hook_views_query_alter examples to get you started:

+4
source

This may no longer be appropriate for you, but there seems to be a very useful discussion on Drupal.org in Implementing a Custom SQL Query for Views / Filters , which seem to answer my similar question.

In particular, the original poster suggested connecting to the hook views_views_pre_execute , which someone else mentioned, can be placed in a custom module, for example:

 function mymodulename_views_pre_execute(&$view) { if($view->name=="groups_list2") { // ... $view->build_info['query'] = "SELECT node.nid AS nid ". "FROM node WHERE node.type='%s'"; // wrapped for legibility } } 

And another poster noted mySQL Views and the Table Wizard ( direct link ), although they mentioned what to keep in mind in this article about mySQL Views performance

Of course, it's worth reading the entire thread on Drupal.org, although I found it really useful; I hope someone else does too.

Update: Indeed, this is exactly what we are doing now, overriding views_views_pre_execute in a custom module to enter new SQL before it gets into the database. I opened a similar (but more specific) question on Drupal.SE in Slow query with a large data set in Drupal views - is it better to handle it in SQL or PHP? which you may find useful.

+1
source

If you want to execute your own SQL (these views cannot generate), for example, calculated fields or complex SQL connections, then you are a user module.

See drupal for guidance to get started.

-1
source

All Articles