From d26bc5fd5814a60120b5bad26cc9a6c1c22bca19 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 24 Apr 2018 12:14:49 -0500 Subject: [PATCH] Patch views_selective_filters for basic search_api_integration --- views_handler_filter_selective.inc | 42 +++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/views_handler_filter_selective.inc b/views_handler_filter_selective.inc index faabe12..5e8b3f4 100644 --- a/views_handler_filter_selective.inc +++ b/views_handler_filter_selective.inc @@ -416,16 +416,44 @@ class views_handler_filter_selective extends views_handler_filter_in_operator { // Create array of objects for selector. $oids = array(); $field_alias = isset($display_field_handler->aliases[$display_field_handler->real_field]) ? $display_field_handler->aliases[$display_field_handler->real_field] : $display_field_handler->table_alias . '_' . $display_field_handler->real_field; + + // Add Search API integration. + if (substr($this->view->base_table, 0, 17) === 'search_api_index_') { + $search_index = search_api_index_load(substr($this->view->base_table, 17)); + $entity_type = $search_index->getEntityType(); + } + foreach ($view_copy->result as $index => $row) { - // $key = $display_field_handler->get_value($row) should be more robust. - // But values are sometimes nested arrays, and we need single values. - // For the filters. - $key = $display_field_handler->get_value($row); - if (is_array($key)) { - $key = $row->{$field_alias}; + // Search API specific code. + if (substr($this->view->base_table, 0, 17) === 'search_api_index_') { + if (!empty($row->_entity_properties['search_api_id'])) { + $entity_array = entity_load($entity_type, array($row->_entity_properties['search_api_id'])); + if (!empty($entity_array)) { + $entity = reset($entity_array); + $entity_wrapper = entity_metadata_wrapper($entity_type, $entity); + $field_view = field_view_field($entity_type, $entity, $display_field_id, array( + 'label' => 'hidden', + )); + $key = $entity_wrapper->getIdentifier();; + $value = strip_tags(render($field_view)); + } + } + } + // Non-Search API code. + else { + // $key = $display_field_handler->get_value($row) should be more robust. + // But values are sometimes nested arrays, and we need single values. + // For the filters. + $key = $display_field_handler->get_value($row); + + if (is_array($key)) { + $key = $row->{$field_alias}; + } + + $value = strip_tags($view_copy->render_field($display_field_id, $index)); } - $value = strip_tags($view_copy->render_field($display_field_id, $index)); $oids[$key] = empty($value) ? t('Empty (@key)', array('@key' => empty($key) ? json_encode($key) : $key)) : $value; + } // Sort values. -- 2.16.2 From e58a47a735c34564cd4dc0a14d86a74f56ac99a7 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 24 Apr 2018 16:42:28 -0500 Subject: [PATCH] Get filter working when it is applied --- views_handler_filter_selective.inc | 66 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/views_handler_filter_selective.inc b/views_handler_filter_selective.inc index 5e8b3f4..ba21c4b 100644 --- a/views_handler_filter_selective.inc +++ b/views_handler_filter_selective.inc @@ -430,11 +430,13 @@ class views_handler_filter_selective extends views_handler_filter_in_operator { $entity_array = entity_load($entity_type, array($row->_entity_properties['search_api_id'])); if (!empty($entity_array)) { $entity = reset($entity_array); - $entity_wrapper = entity_metadata_wrapper($entity_type, $entity); $field_view = field_view_field($entity_type, $entity, $display_field_id, array( 'label' => 'hidden', )); - $key = $entity_wrapper->getIdentifier();; + // @todo: Extend this solution to work with more than just + // taxonomy terms. + // $entity_wrapper = entity_metadata_wrapper($entity_type, $entity); + $key = strip_tags(render($field_view['#items'][0]['tid']));; $value = strip_tags(render($field_view)); } } @@ -550,4 +552,64 @@ class views_handler_filter_selective extends views_handler_filter_in_operator { return $this->originalOptions; } + public function op_simple() { + // Search API specific code. + if (substr($this->view->base_table, 0, 17) === 'search_api_index_') { + watchdog('testing', $this->value); + if ($this->operator === 'empty') { + $this->query->condition($this->real_field, NULL, '=', $this->options['group']); + return; + } + if ($this->operator === 'not empty') { + $this->query->condition($this->real_field, NULL, '<>', $this->options['group']); + return; + } + + // Extract the value. + while (is_array($this->value) && count($this->value) == 1) { + $this->value = reset($this->value); + } + + // Determine operator and conjunction. The defaults are already right for + // "all of". + $operator = '='; + $conjunction = 'AND'; + switch ($this->operator) { + case '=': + $conjunction = 'OR'; + break; + + case '<>': + $operator = '<>'; + break; + } + + // If the value is an empty array, we either want no filter at all (for + // "is none of"), or want to find only items with no value for the field. + if ($this->value === array()) { + if ($operator != '<>') { + $this->query->condition($this->real_field, NULL, '=', $this->options['group']); + } + return; + } + + if (is_scalar($this->value) && $this->value !== '') { + $this->query->condition($this->real_field, $this->value, $operator, $this->options['group']); + } + elseif ($this->value) { + $filter = $this->query->createFilter($conjunction); + // $filter will be NULL if there were errors in the query. + if ($filter) { + foreach ($this->value as $v) { + $filter->condition($this->real_field, $v, $operator); + } + $this->query->filter($filter, $this->options['group']); + } + } + } + else { + parent::op_simple(); + } + } + } -- 2.16.2