- Issue created by @somebodysysop
- 🇺🇸United States somebodysysop
p.s.
This is the general structural guidance I tried to follow to accomplish the custom views search api solr filter:
- https://www.drupal.org/project/search_api/issues/3003129 💬 how to make a custom exposed filter for SOLR view with hook_views_data_alter Fixed
- Status changed to Fixed
11 months ago 8:35am 26 December 2023 - 🇦🇹Austria drunken monkey Vienna, Austria
I don’t think you even need a custom filter, you can just use the existing
SearchApiOptions
filter plugin. Simply usehook_views_data_alter()
to change the “Node Group » Group » Title” field’s filter plugin tosearch_api_options
, plus set an appropriateoptions callback
so the plugin knows the list of available values. (See_search_api_views_handler_adjustments()
, under$type == 'options'
.) That’s all that should be needed.If you do want to use your own filter plugin, you simply need to use one line to add the condition to the query:
$this->getQuery()->addCondition('sbn_add_nodegroup_str', $this->value, 'IN');
Here,
sbn_add_nodegroup_str
is the Search API field ID of the field, no need to make this Solr-specific in any way.
Or you simply let your filter plugin inherit fromSearchApiOptions
and merely override thegetValueOptions()
method so it doesn’t need anoptions callback
set.Hope this helps.
- 🇺🇸United States somebodysysop
@druken monkey
Thank you so much! Worked like a charm. For anyone else who runs into this situation:
First get the label, machine name and path property for the field for which you want a views filter. These can be found in Search API datasource fields. Also get the ID of the index.
In hook_views_data_alter:
//////////////////////////// // node group filter plugin //////////////////////////// // Targeting the specific Search API index and field. if (isset($data['search_api_index_default_solr_index']['label_2'])) { // Change the filter plugin to 'search_api_options'. $data['search_api_index_default_solr_index']['label_2']['filter']['id'] = 'search_api_options'; // Set an options callback to your custom function. $data['search_api_index_default_solr_index']['label_2']['filter']['options callback'] = 'sbn_get_group_titles'; }
The only other thing you'll need is the function to get the group titles (if you don't already have one)
/** * Options callback to provide the list of group titles. */ function sbn_get_group_titles() { $options = []; // Query to load all group entities or use an appropriate method to fetch group titles. $groups = \Drupal::entityTypeManager()->getStorage('group')->loadMultiple(); foreach ($groups as $group) { $options[$group->id()] = $group->label(); } return $options; }
That's it. You should be good to go. Just add the filter to your view:
- 🇺🇸United States somebodysysop
While the SearchApiOptions filter plugin works great for constructing the groups filter, it doesn't filter both nodes and files (even though the files do contain the Solr sbn_add_nodegroup_str field which is created by the Search API processor plugin).
So, I'm thinking the better route is to create my own filter that will modify the Solr query itself. Based upon your suggestions, here is my new search api group node filter class file:
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; use Drupal\views\Plugin\views\filter\StringFilter; use Drupal\views\Plugin\views\filter\ManyToOne; use Drupal\views\Plugin\views\query\Sql; use Drupal\views\Views; use Drupal\views\Plugin\views\relationship\RelationshipPluginBase; use Drupal\Views\Plugin\views\join\JoinPluginBase; use Drupal\views\Plugin\ViewsHandlerManager; use Drupal\search_api\Plugin\views\query\SearchApiQuery; /** * Filters by Node Group. * * @ingroup views_filter_handlers * * @ViewsFilter("node_group_filter") */ class NodeGroupFilter extends SearchApiFilter { /** * The current display. * * @var string * The current display of the view. */ protected $currentDisplay; /** * {@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']; $this->currentDisplay = $view->current_display; } /** * 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)) { $this->getQuery()->addCondition('sbn_add_nodegroup_str', $this->value, 'IN'); } } }
But, views doesn't even recognize it. I can't find it in "Add filter criteria" to select it Can you see what the problem is?
Thanks!
- 🇺🇸United States somebodysysop
So, it turns out the resolution in #4 💬 How to create a custom views filter for a seach api solr view? Fixed did in fact work. It turns out that the site I was testing on was using outdated Group labels. The easy fix was to re-index the site, and the Group filter works as it should.
Automatically closed - issue fixed for 2 weeks with no activity.