/** * Implements FacetapiQueryTypeInterface::build(). * * Unlike normal facets, we provide a static list of options. */ public function build() { $facet = $this->adapter->getFacet($this->facet); $search_ids = drupal_static('search_api_facetapi_active_facets', array()); if (empty($search_ids[$facet['name']]) || !search_api_current_search($search_ids[$facet['name']])) { return array(); } $search_id = $search_ids[$facet['name']]; $build = array(); $search = search_api_current_search($search_id); $results = $search[1]; if (!$results['result count']) { return array(); } // Executes query, iterates over results. if (isset($results['search_api_facets']) && isset($results['search_api_facets'][$this->facet['field']])) { $values = $results['search_api_facets'][$this->facet['field']]; $build = date_facets_get_ranges(); $now = $_SERVER['REQUEST_TIME']; // Calculate values by facet. foreach ($values as $value) { $value['filter'] = str_replace('"', '', $value['filter']); $diff = $now - $value['filter']; foreach ($build as $key => $item) { if ($diff < $item['#time_interval']) { $build[$key]['#count'] += $value['count']; } } } } // Unset empty items. foreach ($build as $key => $item) { if ($item['#count'] === NULL) { unset($build[$key]); } } // Gets total number of documents matched in search. $total = $results['result count']; $keys_of_active_facets = array(); // Gets active facets, starts building hierarchy. foreach ($this->adapter->getActiveItems($this->facet) as $key => $item) { // If the item is active, the count is the result set count. $build[$key]['#count'] = $total; $keys_of_active_facets[] = $key; } // If we have active item, unset other items. $settings = $facet->getSettings()->settings; if (isset($settings['operator']) && $settings['operator'] !== FACETAPI_OPERATOR_OR) { if (!empty($keys_of_active_facets)) { foreach ($build as $key => $item) { if (!in_array($key, $keys_of_active_facets)) { unset($build[$key]); } } } } return $build; }
/** * {@inheritdoc} */ public function cacheGet($type) { if ($type != 'results') { return parent::cacheGet($type); } // Values to set: $view->result, $view->total_rows, $view->execute_time, // $view->current_page. if ($cache = \Drupal::cache($this->resultsBin)->get($this->generateResultsKey())) { $cutoff = $this->cacheExpire($type); if (!$cutoff || $cache->created > $cutoff) { $this->view->result = $cache->data['result']; $this->view->total_rows = $cache->data['total_rows']; $this->view->setCurrentPage($cache->data['current_page']); $this->view->execute_time = 0; // Trick Search API into believing a search happened, to make facetting // et al. work. $query = $this->getQuery()->getSearchApiQuery(); // @todo Find a replacement for that. Also set it on the Views Query? search_api_current_search($query->getOption('search id'), $query, $cache->data['search_api results']); return TRUE; } } return FALSE; }
/** * Implements FacetapiQueryTypeInterface::build(). * * Unlike normal facets, we provide a static list of options. */ public function build() { $facet = $this->adapter->getFacet($this->facet); $search_ids = drupal_static('search_api_facetapi_active_facets', array()); if (empty($search_ids[$facet['name']]) || !search_api_current_search($search_ids[$facet['name']])) { return array(); } $search_id = $search_ids[$facet['name']]; $build = array(); $search = search_api_current_search($search_id); $results = $search[1]; if (!$results['result count']) { return array(); } // Executes query, iterates over results. if (isset($results['search_api_facets']) && isset($results['search_api_facets'][$this->facet['field']])) { $values = $results['search_api_facets'][$this->facet['field']]; $realm = facetapi_realm_load('block'); $settings = $this->adapter->getFacetSettings($this->facet, $realm); $ranges = isset($settings->settings['ranges']) && !empty($settings->settings['ranges']) ? $settings->settings['ranges'] : date_facets_default_ranges(); // Build the markup for the facet's date ranges. $build = date_facets_get_ranges_render_arrays($ranges); // Calculate values by facet. foreach ($values as $value) { $value['filter'] = str_replace('"', '', $value['filter']); foreach ($ranges as $key => $item) { list($start, $end) = $this->generateRange($item); $future_interval = $item['date_range_end_op'] == '+' && $start <= $value['filter'] && $value['filter'] <= $end; $past_interval = $item['date_range_start_op'] == '-' && $start <= $value['filter'] && $value['filter'] <= $end; if ($future_interval || $past_interval) { $build[$key]['#count'] += $value['count']; } } } } // Unset empty items. foreach ($build as $key => $item) { if ($item['#count'] === NULL) { unset($build[$key]); } } // Gets total number of documents matched in search. $total = $results['result count']; $keys_of_active_facets = array(); // Gets active facets, starts building hierarchy. foreach ($this->adapter->getActiveItems($this->facet) as $key => $item) { // If the item is active, the count is the result set count. $build[$key]['#count'] = $total; $keys_of_active_facets[] = $key; } // If we have active item, unset other items. $settings = $facet->getSettings()->settings; if (isset($settings['operator']) && $settings['operator'] !== FACETAPI_OPERATOR_OR) { if (!empty($keys_of_active_facets)) { foreach ($build as $key => $item) { if (!in_array($key, $keys_of_active_facets)) { unset($build[$key]); } } } } return $build; }