1

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 as I want to filter it per page, see info below how I am doing that.

enter image description here

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.

enter image description here

The code I have so far after using Xdebug is:

use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;


/**
* 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 (no views results show up on the page) and I am not sure what I am doing wrong.

Here is the output from Xdebug as well if that helps.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.