0

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...

2 Answers 2

0

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';
        }
      }
    }
  }

-1

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.

1
  • I don't think so.
    – mankin
    Apr 18, 2023 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.