Score:0

Change operator in a query?

am flag

I'm working in a Drupal 9 website.

I need to change operator in my view query from != to <. In a view i got two contextuals filters:

  • The first to retrieve id from an entity reference field.
  • The second to retrieve current node id.

In the second contextual filter, exclude is selected to be != in my query.

The result i need is to transform this != to < for show all nodes who got the same reference field ID AND an ID lower than current node id. It's like a previous Articles by the same reference field!

My view query is :

SELECT "field_entreprises_marque_node_field_data"."created" AS "field_entreprises_marque_node_field_data_created", "node_field_data"."nid" AS "nid", "field_entreprises_marque_node_field_data"."nid" AS "field_entreprises_marque_node_field_data_nid"
FROM
{node_field_data} "node_field_data"
LEFT JOIN {node__field_entreprises_marque} "node__field_entreprises_marque" ON node_field_data.nid = node__field_entreprises_marque.field_entreprises_marque_target_id AND node__field_entreprises_marque.deleted = '0'
LEFT JOIN {node_field_data} "field_entreprises_marque_node_field_data" ON node__field_entreprises_marque.entity_id = field_entreprises_marque_node_field_data.nid
WHERE (("node_field_data"."status" = '1') AND ("node_field_data"."type" IN ('entreprise_marque'))) AND ((node_field_data.nid = '3259' )) AND ((field_entreprises_marque_node_field_data.nid != '22561' OR field_entreprises_marque_node_field_data.nid IS NULL))
ORDER BY "field_entreprises_marque_node_field_data_created" DESC
LIMIT 20 OFFSET 0

What i need is : field_entreprises_marque_node_field_data.nid < '22561'

I try like this:

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) {
  if ($view->current_display === '20_articles_precedents') {
    $view->query->where[4]["conditions"][0]["operator"] = '>';
  }
}

But nothing appens...

Score:0
am flag

I do it like that:

/**
 * Implements hook_views_query_alter().
 * modifier la requete d'une vue
 */
function injector_views_query_alter(&$view, &$query) {

  if ($view->id() == '20_articles_precedents' && $view->current_display == 'block_1') {
    $field_name = 'field_entreprises_marque_node_field_data.nid != :nid2 OR field_entreprises_marque_node_field_data.nid IS NULL';
    foreach ($query->where as &$condition_group) {
      foreach ($condition_group['conditions'] as &$condition) {
        if ($condition['field'] === $field_name) {
          $condition['field'] = 'field_entreprises_marque_node_field_data.nid < :nid2 OR field_entreprises_marque_node_field_data.nid IS NULL';
        }
      }
    }
  }

Score:-1
nz flag

I'm not sure, but i think you need to implement the hook_views_api():

/**
 * Implementation of hook_views_api().
 */
function YOUR_MODULE_views_api() { 
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'YOUR_MODULE_NAME'),
  );
}

Besides that on your hook I think it's not necessary pass the operator selector, like this:

if ($view->current_display === '20_articles_precedents') {
  $view->query->where[4]["conditions"] = '>';
}

But I dont't run any test of this.

mankin avatar
am flag
I don't think so.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.