1

Below I have a View. I have the following hook. I'm hoping to modify the WHERE clause of the view, but the following code seems not to be called.

function jch_utilities_query_alter(QueryAlterableInterface $the_query) {
  if ($the_query->hasTag('jch')) {
    dsm('Found it!');
    dsm($the_query);        
  }
}

I wanted to use hook_views_query_alter() but I read that it doesn't work with indexed content. So I was trying this. I also couldn't get Views PHP to work (bug with $row).

I just want a way to modify $query->where and I keep getting blocked at every turn. Any suggestions?

$view = new view();
$view->name = 'collection_products';
$view->description = '';
$view->tag = 'jch';
$view->base_table = 'search_api_index_product_display';
$view->human_name = 'Collection products';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = '%1';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['style_plugin'] = 'list';
$handler->display->display_options['style_options']['grouping'] = array(
  0 => array(
    'field' => 'name',
    'rendered' => 1,
    'rendered_strip' => 0,
  ),
);
$handler->display->display_options['style_options']['class'] = 'products-per-collection';
$handler->display->display_options['row_plugin'] = 'fields';
/* Header: Global: View area */
$handler->display->display_options['header']['view']['id'] = 'view';
$handler->display->display_options['header']['view']['table'] = 'views';
$handler->display->display_options['header']['view']['field'] = 'view';
$handler->display->display_options['header']['view']['view_to_insert'] = 'collection_products:block_1';
$handler->display->display_options['header']['view']['inherit_arguments'] = TRUE;
/* Relationship: Category */
$handler->display->display_options['relationships']['field_category']['id'] = 'field_category';
$handler->display->display_options['relationships']['field_category']['table'] = 'search_api_index_product_display';
$handler->display->display_options['relationships']['field_category']['field'] = 'field_category';
$handler->display->display_options['relationships']['field_category']['ui_name'] = 'Category';
/* Field: Taxonomy term: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'entity_taxonomy_term';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['relationship'] = 'field_category';
$handler->display->display_options['fields']['name']['label'] = '';
$handler->display->display_options['fields']['name']['exclude'] = TRUE;
$handler->display->display_options['fields']['name']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['name']['link_to_entity'] = 0;
/* Field: Content: Rendered Content */
$handler->display->display_options['fields']['rendered_entity']['id'] = 'rendered_entity';
$handler->display->display_options['fields']['rendered_entity']['table'] = 'views_entity_node';
$handler->display->display_options['fields']['rendered_entity']['field'] = 'rendered_entity';
$handler->display->display_options['fields']['rendered_entity']['label'] = '';
$handler->display->display_options['fields']['rendered_entity']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['rendered_entity']['link_to_entity'] = 1;
$handler->display->display_options['fields']['rendered_entity']['display'] = 'view';
$handler->display->display_options['fields']['rendered_entity']['view_mode'] = 'product_list';
/* Sort criterion: Category: Search Rank (indexed) */
$handler->display->display_options['sorts']['field_category_field_search_rank']['id'] = 'field_category_field_search_rank';
$handler->display->display_options['sorts']['field_category_field_search_rank']['table'] = 'search_api_index_product_display';
$handler->display->display_options['sorts']['field_category_field_search_rank']['field'] = 'field_category_field_search_rank';
/* Contextual filter: Indexed Content: Collection */
$handler->display->display_options['arguments']['field_collection']['id'] = 'field_collection';
$handler->display->display_options['arguments']['field_collection']['table'] = 'search_api_index_product_display';
$handler->display->display_options['arguments']['field_collection']['field'] = 'field_collection';
$handler->display->display_options['arguments']['field_collection']['default_action'] = 'not found';
$handler->display->display_options['arguments']['field_collection']['default_argument_type'] = 'fixed';
$handler->display->display_options['arguments']['field_collection']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['field_collection']['summary_options']['grouping'] = array(
  0 => array(
    'field' => 'name',
    'rendered' => 1,
    'rendered_strip' => 0,
  ),
);
$handler->display->display_options['arguments']['field_collection']['specify_validation'] = TRUE;
$handler->display->display_options['arguments']['field_collection']['validate']['type'] = 'taxonomy_term';
$handler->display->display_options['arguments']['field_collection']['validate_options']['vocabularies'] = array(
  'collection' => 'collection',
);
$handler->display->display_options['arguments']['field_collection']['break_phrase'] = 0;
/* Filter criterion: Indexed Content: Status */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'search_api_index_product_display';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = array(
  1 => '1',
);
$handler->display->display_options['filters']['status']['group'] = 1;
/* Filter criterion: Indexed Content: Content type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'search_api_index_product_display';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'bags_cases' => 'bags_cases',
  'basic_products' => 'basic_products',
);
$handler->display->display_options['filters']['type']['group'] = 1;

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['title'] = '%1';
$handler->display->display_options['defaults']['hide_admin_links'] = FALSE;
$handler->display->display_options['defaults']['fields'] = FALSE;
/* Field: Taxonomy term: Name */
$handler->display->display_options['fields']['name_1']['id'] = 'name_1';
$handler->display->display_options['fields']['name_1']['table'] = 'entity_taxonomy_term';
$handler->display->display_options['fields']['name_1']['field'] = 'name';
$handler->display->display_options['fields']['name_1']['relationship'] = 'field_category';
$handler->display->display_options['fields']['name_1']['label'] = '';
$handler->display->display_options['fields']['name_1']['exclude'] = TRUE;
$handler->display->display_options['fields']['name_1']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['name_1']['link_to_entity'] = 0;
$handler->display->display_options['fields']['name_1']['replace_spaces'] = TRUE;
/* Field: Taxonomy term: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'entity_taxonomy_term';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['relationship'] = 'field_category';
$handler->display->display_options['fields']['name']['label'] = '';
$handler->display->display_options['fields']['name']['exclude'] = TRUE;
$handler->display->display_options['fields']['name']['alter']['alter_text'] = TRUE;
$handler->display->display_options['fields']['name']['alter']['text'] = '<span id="[name_1]"></span><div class="collection"><a>[name]</a></div>';
$handler->display->display_options['fields']['name']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['name']['link_to_entity'] = 0;
/* Field: Content: Rendered Content */
$handler->display->display_options['fields']['rendered_entity']['id'] = 'rendered_entity';
$handler->display->display_options['fields']['rendered_entity']['table'] = 'views_entity_node';
$handler->display->display_options['fields']['rendered_entity']['field'] = 'rendered_entity';
$handler->display->display_options['fields']['rendered_entity']['label'] = '';
$handler->display->display_options['fields']['rendered_entity']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['rendered_entity']['link_to_entity'] = 0;
$handler->display->display_options['fields']['rendered_entity']['display'] = 'view';
$handler->display->display_options['fields']['rendered_entity']['view_mode'] = 'product_list';
/* Field: Collection: Description (indexed) */
$handler->display->display_options['fields']['field_collection_description']['id'] = 'field_collection_description';
$handler->display->display_options['fields']['field_collection_description']['table'] = 'search_api_index_product_display';
$handler->display->display_options['fields']['field_collection_description']['field'] = 'field_collection_description';
$handler->display->display_options['fields']['field_collection_description']['label'] = '';
$handler->display->display_options['fields']['field_collection_description']['exclude'] = TRUE;
$handler->display->display_options['fields']['field_collection_description']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_collection_description']['link_to_entity'] = 0;
/* Field: Category: Search Rank (indexed) */
$handler->display->display_options['fields']['field_category_field_search_rank']['id'] = 'field_category_field_search_rank';
$handler->display->display_options['fields']['field_category_field_search_rank']['table'] = 'search_api_index_product_display';
$handler->display->display_options['fields']['field_category_field_search_rank']['field'] = 'field_category_field_search_rank';
$handler->display->display_options['fields']['field_category_field_search_rank']['label'] = '';
$handler->display->display_options['fields']['field_category_field_search_rank']['exclude'] = TRUE;
$handler->display->display_options['fields']['field_category_field_search_rank']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_category_field_search_rank']['settings'] = array(
  'thousand_separator' => ' ',
  'prefix_suffix' => 1,
);
/* Field: Indexed Content: Collection */
$handler->display->display_options['fields']['field_collection']['id'] = 'field_collection';
$handler->display->display_options['fields']['field_collection']['table'] = 'search_api_index_product_display';
$handler->display->display_options['fields']['field_collection']['field'] = 'field_collection';
$handler->display->display_options['fields']['field_collection']['label'] = '';
$handler->display->display_options['fields']['field_collection']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_collection']['link_to_entity'] = 0;
$handler->display->display_options['fields']['field_collection']['display'] = 'id';
$handler->display->display_options['fields']['field_collection']['view_mode'] = 'full';
$handler->display->display_options['fields']['field_collection']['bypass_access'] = 0;
$handler->display->display_options['defaults']['filter_groups'] = FALSE;
$handler->display->display_options['defaults']['filters'] = FALSE;
/* Filter criterion: Indexed Content: Status */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'search_api_index_product_display';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = array(
  1 => '1',
);
$handler->display->display_options['filters']['status']['group'] = 1;
/* Filter criterion: Indexed Content: Content type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'search_api_index_product_display';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'bags_cases' => 'bags_cases',
  'basic_products' => 'basic_products',
);
$handler->display->display_options['filters']['type']['group'] = 1;
$handler->display->display_options['path'] = 'collection/%';

/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['defaults']['css_class'] = FALSE;
$handler->display->display_options['css_class'] = 'collection-terms';
$handler->display->display_options['defaults']['hide_admin_links'] = FALSE;
$handler->display->display_options['defaults']['style_plugin'] = FALSE;
$handler->display->display_options['style_plugin'] = 'list';
$handler->display->display_options['style_options']['grouping'] = array(
  0 => array(
    'field' => 'name_1',
    'rendered' => 0,
    'rendered_strip' => 1,
  ),
);
$handler->display->display_options['style_options']['default_row_class'] = FALSE;
$handler->display->display_options['style_options']['row_class_special'] = FALSE;
$handler->display->display_options['style_options']['class'] = 'head-test';
$handler->display->display_options['defaults']['style_options'] = FALSE;
$handler->display->display_options['defaults']['row_plugin'] = FALSE;
$handler->display->display_options['row_plugin'] = 'fields';
$handler->display->display_options['defaults']['row_options'] = FALSE;
$handler->display->display_options['defaults']['header'] = FALSE;
/* Header: Global: Text area */
$handler->display->display_options['header']['area']['id'] = 'area';
$handler->display->display_options['header']['area']['table'] = 'views';
$handler->display->display_options['header']['area']['field'] = 'area';
$handler->display->display_options['header']['area']['content'] = '<h2>On This Page</h2>';
$handler->display->display_options['header']['area']['format'] = 'full_html';
$handler->display->display_options['defaults']['fields'] = FALSE;
/* Field: Taxonomy term: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'entity_taxonomy_term';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['relationship'] = 'field_category';
$handler->display->display_options['fields']['name']['label'] = '';
$handler->display->display_options['fields']['name']['exclude'] = TRUE;
$handler->display->display_options['fields']['name']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['name']['link_to_entity'] = 0;
$handler->display->display_options['fields']['name']['replace_spaces'] = TRUE;
/* Field: Taxonomy term: Name */
$handler->display->display_options['fields']['name_1']['id'] = 'name_1';
$handler->display->display_options['fields']['name_1']['table'] = 'entity_taxonomy_term';
$handler->display->display_options['fields']['name_1']['field'] = 'name';
$handler->display->display_options['fields']['name_1']['relationship'] = 'field_category';
$handler->display->display_options['fields']['name_1']['label'] = '';
$handler->display->display_options['fields']['name_1']['exclude'] = TRUE;
$handler->display->display_options['fields']['name_1']['alter']['alter_text'] = TRUE;
$handler->display->display_options['fields']['name_1']['alter']['text'] = '<a href="#[name]">[name_1]</a>';
$handler->display->display_options['fields']['name_1']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['name_1']['link_to_entity'] = 0;
/* Field: Global: Custom text */
$handler->display->display_options['fields']['nothing']['id'] = 'nothing';
$handler->display->display_options['fields']['nothing']['table'] = 'views';
$handler->display->display_options['fields']['nothing']['field'] = 'nothing';
$handler->display->display_options['fields']['nothing']['label'] = '';
$handler->display->display_options['fields']['nothing']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['nothing']['hide_empty'] = TRUE;
/* Field: Collection: Description (indexed) */
$handler->display->display_options['fields']['field_collection_description']['id'] = 'field_collection_description';
$handler->display->display_options['fields']['field_collection_description']['table'] = 'search_api_index_product_display';
$handler->display->display_options['fields']['field_collection_description']['field'] = 'field_collection_description';
$handler->display->display_options['fields']['field_collection_description']['label'] = '';
$handler->display->display_options['fields']['field_collection_description']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_collection_description']['link_to_entity'] = 0;
/* Field: Category: Search Rank (indexed) */
$handler->display->display_options['fields']['field_category_field_search_rank']['id'] = 'field_category_field_search_rank';
$handler->display->display_options['fields']['field_category_field_search_rank']['table'] = 'search_api_index_product_display';
$handler->display->display_options['fields']['field_category_field_search_rank']['field'] = 'field_category_field_search_rank';
$handler->display->display_options['fields']['field_category_field_search_rank']['label'] = '';
$handler->display->display_options['fields']['field_category_field_search_rank']['exclude'] = TRUE;
$handler->display->display_options['fields']['field_category_field_search_rank']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_category_field_search_rank']['settings'] = array(
  'thousand_separator' => '',
  'prefix_suffix' => 1,
);
$translatables['collection_products'] = array(
  t('Master'),
  t('%1'),
  t('more'),
  t('Apply'),
  t('Reset'),
  t('Sort by'),
  t('Asc'),
  t('Desc'),
  t('Category'),
  t('All'),
  t('Page'),
  t('<span id="[name_1]"></span><div class="collection"><a>[name]</a></div>'),
  t('Block'),
  t('<h2>On This Page</h2>'),
  t('<a href="#[name]">[name_1]</a>'),
);
0

I've never used tag but it should work, name works and that's usually where I change mine.

/**
 * Implements hook_views_query_alter(). 
 */ 
function MODULENAME_views_query_alter(&$view, &$query) { 
  // Alter query for 'OR'. 
  if ($view->name === 'VIEW_NAME') {
    $query->where[0]['type'] = 'OR';
  }
}

For reference: hook_views_query_alter()

edit

I guess I should have read closer, the view is integrated with search API. So you have to use hook_search_api_query_alter() I believe.

function hook_search_api_query_alter(SearchApiQueryInterface $query) {
  $info = entity_get_info($index->item_type);
  $query->condition($info['entity keys']['id'], 0, '!=');
}

More info

  • Unfortunately, this view does not get fired with hook_views_query_alter(). Apparently that is the case for views which use Indexed content as I wrote above. Are you using Commerce and the Search API? collection_products? – jchwebdev Feb 7 '15 at 21:02
  • Have you tried this alter? drupalcontrib.org/api/drupal/… – burnsjeremy Feb 7 '15 at 21:09
  • Here's another link: drupal.org/node/2179003 – burnsjeremy Feb 7 '15 at 21:17
  • Thanks, I'll give it a shot. Some days... I truly -hate- Drupal. Or rather... the absolutely crap documentation. – jchwebdev Feb 8 '15 at 3:14
  • PS... What's weird: hook_views_pre_render() DOES work with this View. Why oh -why- would that work and hook_views_query_alter -not- work? That's the kind of thing that drives me NUTS. – jchwebdev Feb 8 '15 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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