how to make a custom exposed filter for SOLR view with hook_views_data_alter

Created on 28 September 2018, about 6 years ago
Updated 2 January 2024, 11 months ago

I need to make a custom exposed filter for a view , and I found an example that does it for nodes with hook_views_data_alter , but the filter does not show up for my view which is using SOLR so it has 'content datasource' instead of node content.
The filter in this example [https://www.webomelette.com/creating-custom-views-filter-drupal-8] works to add filters to a view with regular node content.

What do I need to change in this code to make it work for content datasource

πŸ’¬ Support request
Status

Fixed

Version

1.6

Component

General code

Created by

πŸ‡ΊπŸ‡ΈUnited States dianacastillo Miami

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡΅πŸ‡°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

Production build 0.71.5 2024