base_table == 'node' && empty($view->disabled) && $view->display['default']->display_options['use_pager']) { $styles[$view->name] = new faceted_search_views_style($view->name); } } return $styles; } /** * Implementation of hook_views_query_alter(). */ function faceted_search_views_views_query_alter (&$view, &$query) { if (is_array($view->args[0]) && isset($view->args[0]['faceted_search'])) { $search = $view->args[0]['faceted_search']; if ($search->ready()) { if ($search->get_results_count() > 0) { $join = new views_join(); $join->construct($search->get_results_table(), 'node', 'nid', 'nid', array(), 'INNER'); $query->add_table($search->get_results_table(), NULL, $join); } else { $query->add_where(0, 'FALSE'); // Ensure the view shows no results. } } } } /** * Implementation of hook_views_pre_execute(). */ function faceted_search_views_views_pre_execute(&$view) { if (is_array($view->args[0]) && isset($view->args[0]['faceted_search']) && $view->current_display == 'default') { if (empty($view->pager['items_per_page'])) { // Make sure a limit is set. $view->set_items_per_page(variable_get('default_nodes_main', 10)); } if (empty($view->pager['use_pager'])) { $view->set_use_pager(TRUE); } } } /** * Provides a view-based display style for search results. */ class faceted_search_views_style extends faceted_search_ui_style { // Name of the view to use with this style. var $_name; /** * Constructor. * * @param $view_name * Name of the view to use with this style. */ function faceted_search_views_style($view_name) { $this->_name = $view_name; } /** * Return the name of this style. */ function get_label() { return t('Views: @view', array('@view' => $this->_name)); } /** * Apply the view's count query as a subquery to filter the search results. * * Note: We use the view's count query for this purpose because we don't need * the sorting and field selection provided by view's full query. The view's * only purpose here is to filter the search results. */ function query_alter(&$query, $search) { $view = views_get_view($this->_name); $view->set_display('default'); $view->set_arguments(array('faceted_search' => $search)); $view->build('default'); $views_query = db_rewrite_sql($view->build_info['count_query'], $view->base_table, $view->base_field, array('view' => &$view)); $views_args = $view->build_info['query_args']; $views_replacements = module_invoke_all('views_query_substitutions', $view); $views_query = str_replace(array_keys($views_replacements), $views_replacements, $views_query); if (is_array($views_args)) { foreach ($views_args as $id => $arg) { $views_args[$id] = str_replace(array_keys($views_replacements), $views_replacements, $arg); } } $query->add_subquery('n.nid IN ('. $views_query .')', $views_args); } /** * Format the search results according to this style's desire. */ function format_results($search) { if ($view = views_get_view($this->_name)) { if ($view->access('default')) { $output = views_embed_view($this->_name, 'default' , array('faceted_search' => $search)); $view->destroy(); } else { $view->destroy(); drupal_access_denied(); exit(); } } return $output; } }