I am trying to add a “Like” operator via views query alter for my taxonomy term name. My view is a block setup with a contextual filter set to show all results as shown below.
Then within given pages, I’d like my content editors to be able to input parts of a term name (thus the SQL “Like” operator) to filter a view via the paragraphs view field contextual options field. For example part of a term name would be “Creative Commons” as shown below.
The code I have so far derived from Xdebug is:
use DrupalviewsPluginviewsqueryQueryPluginBase;
use DrupalviewsViewExecutable;
/**
* Implements hook_views_query_alter().
*/
function mymodule_custom_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
// Define the view display.
if ($view->current_display === 'remix_icons_nf_block') {
// Define the taxonomy term name.
$name = $view->argument("name")->query->fields("taxonomy_term_field_data_name")("field");
// Add a like operator.
$view->query->where(1)("conditions")(0)("operator") = 'LIKE';
// Now query the name using the condition above.
$view->query->where(1)('conditions')(0)('value') = '%' . $name . '%';
}
}
However, this does not seem to be working and I am not sure what I am doing wrong.