- π΅π°Pakistan Ahmed.Raza
Hi dianacastillo, I know its super long time ago but by any chance do you remember how did you achieve this? I am facing similar problem any help will be highly appreciated.
- πΊπΈUnited States somebodysysop
I honestly thought this was the way to do it:
<?php namespace Drupal\sbn\Plugin\views\filter; use Drupal\search_api\Plugin\views\filter\SearchApiFilter; use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\ViewExecutable; /** * Filters by Node Group. * * @ingroup views_filter_handlers * * @ViewsFilter("search_api_node_group") */ class SearchApiNodeGroupFilter extends SearchApiFilter { /** * {@inheritdoc} */ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) { parent::init($view, $display, $options); $this->valueTitle = t('Search API Node Groups'); $this->definition['options callback'] = [$this, 'generateOptions']; } /** * Helper function that generates the options. * * @return array * An array of group labels. */ public function generateOptions() { // Assuming sbn_get_group_labels() returns an associative array of group labels return sbn_get_group_labels(); } /** * {@inheritdoc} */ public function query() { if (!empty($this->value) && is_array($this->value)) { // Retrieve the underlying Solr query. $solr_query = $this->query->getSolrQuery(); // Construct the filter query. $filter_query = 'sm_sbn_add_nodegroup_str:('; $filter_queries = []; foreach ($this->value as $val) { // Escape special characters in Solr query syntax. $escaped_val = $solr_query->getHelper()->escapePhrase($val); $filter_queries[] = '"' . $escaped_val . '"'; } $filter_query .= implode(' OR ', $filter_queries) . ')'; // Add the filter query to the Solr query. $solr_query->addFilterQuery(['key' => 'sm_sbn_add_nodegroup_str', 'query' => $filter_query]); } } }
But Views won't even recognize it as a filter. I know sm_sbn_add_nodegroup_str is the correct solrid, so I don't know what else to try. Open to suggestions!
- πΊπΈUnited States somebodysysop
This was my solution to this problem, using hook_views_data_alter(): https://www.drupal.org/project/search_api/issues/3409919 π¬ How to create a custom views filter for a seach api solr view? Fixed