0

I am looking for specific difference in working and implementation of both

closed as too broad by kiamlaluno Sep 28 '15 at 3:56

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Welcome to Drupal Answers! If you have a specific concert, then ask a question about your concern. A question like What is the difference between A and B? is too generic. Answers would just repeat what the documentation says, and we aren't here to copy the documentation. – kiamlaluno Sep 28 '15 at 4:01
  • @kiamlaluno I understand your concern keeping the question undeleted as the answer below is good for me next time will keep this in mind not to ask open questions – Kaustubh Bhagwat Sep 28 '15 at 9:02
5
HOOK_QUERY_ALTER:

Perform alterations to a structured query.

ONLY queries that have already been tagged (at creation time) can be caught by this hook. If the query you are hoping to alter doesn't already have a tag (which effectively 'publishes' its willingness to let you mess with it) then you can't alter it using this method.

To find out - inspect the source code of the query you are trying to modify.

HOOK_VIEWS_QUERY_ALTER :

Alter the query before executing the query.

It took me a good bit of time to figure out how to use hook_views_query_alter to modify my view query so I just want to pass on some small bit of knowledge to others who may find it helpful. I wanted to create a view/block that displayed a list of books on my site. I only wanted to list the names of parent book pages not all book pages just book titles. Out of the box, the views module can create a view to display nodes of type "book" but that includes all book pages. The query looked like this:

SELECT node.created AS node_created, node.nid AS nid
FROM
node
WHERE (( (node.status = '1') AND (node.type IN  ('book')) ))
ORDER BY node_created DESC
LIMIT 10 OFFSET 0

What I really wanted was below query (that joined on the book table using book.nid):

SELECT node.created AS node_created, node.nid AS nid
FROM
node
LEFT JOIN book ON node.nid = book.nid
WHERE (( (node.status = '1') AND (node.type IN  ('book')) AND (book.nid = book.bid) ))
ORDER BY node_created DESC
LIMIT 10 OFFSET 0

I was able to create the latter query using the hook_views_query_alter. I spend most of the day reading about the views_plugin_query_default api and views_object and tried several query object function calls. Along the trip, I learned how to use the devel module and dpm to display my query in the admin. I tried all kinds of stuff and ended on this two line module.

function my_hook_views_query_alter(&$view, &$query){
  if ($view->name == 'my_hook') {
    $alias = $query->add_table('book','node');
    $query->add_where_expression(0,'book.nid = book.bid');
    dpm($query); //dumps object to admin with devel module
  }
}

The add_table call automatically analyzed the relationship and created the LEFT JOIN above. I had played around with the views_plugin_query_default::add_where but the result was only a simple where clause that only allowed a scalar value (e.g. book.nid = 'book.bid'). The add_where_expression uses the condition type 'formula' to create a complex where. The resulting where clause that just joins on book.bid was :

WHERE (( (node.status = '1') AND (node.type IN  ('book')) AND (book.nid  = book.bid) ))
1

Hook_query_alter

 hook_query_alter(QueryAlterableInterface $query)

This query is used to alter simply system queries. It requires one parameter $query that describes the composite part of a SQL query. For more details check the drupal docs.

Hook_views_query_alter:

hook_views_query_alter(&$view, &$query)

Actually this is used to alter the query before executing the views query. It requires two parameters &$view and &$query . Here &$query is an object that describe the query and &$view is the view object about to be processed. For further information check at drupal docs.

0

hook_views_query_alter

This hook should be placed in MODULENAME.views.inc and it will be auto-loaded. MODULENAME.views.inc must be in the directory specified by the 'path' key returned by MODULENAME_views_api(), or the same directory as the .module file, if 'path' is unspecified.

how to use hook_query_alter

Structured (aka dynamic) queries that have tags associated may be altered by any module before the query is executed.

How to use hook_query_alter

For more details

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