Skip to main content
10 of 10
replaced http://drupal.stackexchange.com/ with https://drupal.stackexchange.com/

Please read hook_views_query_alter() API, which say:

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.

So double check that you're placing the hook in the correct place either in the main module file or MODULENAME.views.inc (not mymodule_views.inc) and make sure that you've MODULENAME_views_api function defined (unless it's already defined by Feature). After all that clear your all caches (including memcached if in it's in use).

See also: How do I use hook_views_query_alter()?

If there is still a problem, that means your view is implementing different query handler and possibly it implements views_plugin_query (which by default doesn't provide any alter code), so you have to find the right plugin query handler (a query class) and see which hooks they're using instead.

To find which plugin is in use.

Method 1: If view is already exported, check base_table name and which module is dealing with that table. If it's search_api_, then it's Search API module.

Method 2: Edit views/includes/view.inc, find the code which executes the query alter: $this->query->alter($this); and dump the $this->query object to see what class is used for that view.

Then find that class and its alter() method to see how it's implemented.

Here are few examples:

Views (class views_plugin_query_default extends views_plugin_query):

function alter(&$view) {
  foreach (module_implements('views_query_alter') as $module) {
    $function = $module . '_views_query_alter';
    $function($view, $this);
  }
} 

Search API (class SearchApiViewsQuery extends views_plugin_query):

public function alter(&$view) {
  parent::alter($view);
  drupal_alter('search_api_views_query', $view, $this);
}

YQL Views (class yql_views_query_plugin_query_yql extends views_plugin_query):

function alter(&$view) {
  foreach (module_implements('yql_views_query_query_alter') as $module) {
    $function = $module . '_yql_views_query_query_alter';
    $function($view, $this);
  }
}

So as you can see, you need to use different hooks for different views types.

In your particular case collection_products views is handled by Search API, so you need to define hook_search_api_views_query_alter instead.

kenorb
  • 14.3k
  • 10
  • 98
  • 168