1

I'm using Commerce Kickstart 2 and I want to modify the query of the included view collection_products.

As a start, I've added a mymodule_views.inc file with this code:

function jch_utilities_views_query_alter(&$view, &$query) {
  dpm($view->name);
  if ($view->name == 'collection_products') {
    // my code goes here;
  }
}

dpm() fires for 3 times for each view on the page, but not for the collection_products view. Obviously, hook_views_query_alter() is firing, but why isn't that view showing up?

Just to be clear: The view / page display renders fine.

I saw this issue and I wonder if it applies.

Is there another way to modify the query?

1 Answer 1

3

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.

1
  • 1
    Thanks for this, I have a search api view and was going mad trying to figure this out!
    – Chris
    Commented Aug 28, 2018 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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