Hi Team,

Modify sorting criteria using views_query_alter

I want to add numbering sort for text column, so I have added lpad formula in order by field. but it escaping the characters "(", "," in the formula

I am using hook view_query_alter, module code is below,
 

function mymodule_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($view->id() == 'view' && $view->current_display == 'display') {
    // Traverse through the 'orderby in static_id' part of the query.
    foreach ($query->orderby as &$orderby) {
      $pos = strpos($orderby['field'], 'field_static_id_value');
      if ($pos) {
        $orderby['field'] = sprintf("lpad( %s , 20 , 0 ) ", $orderby['field']);
      }
    }
}

Kindly help that I am doing correct way or any another way to add this formula in view query.

Comments

shrikant.dhotre’s picture

Certainly! In Drupal, using hook_views_query_alter to change the sorting based on a formula involves altering the query by implementing logic to dynamically set the sorting criteria. This can be achieved by manipulating the query object within the hook.

Here’s an example of how you might use hook_views_query_alter to change the sorting of a View based on a formula:

/**
 * Implements hook_views_query_alter().
 */
function YOUR_MODULE_NAME_views_query_alter(&$view, &$query) {
  // Check if this is the view you want to modify.
  if ($view->name == 'your_view_name') {
    // Check if the display is the one you want to modify (e.g., page, block, etc.).
    if ($view->current_display == 'your_display_name') {
      // Manipulate the sorting based on a formula.
      $new_ordering = your_custom_sorting_formula(); // Call your custom function to determine the sorting order.

      // Set the new ordering in the query.
      $query->orderby = $new_ordering;
    }
  }
}

/**
 * Custom function to define the sorting order based on your formula.
 */
function your_custom_sorting_formula() {
  // Implement your logic to determine the sorting order.
  // This function should return an array defining the sorting order.

  // Example: Sort by a computed value or any custom logic.
  $sorting_order = array(
    0 => array(
      'field' => 'your_field_name', // Replace with your field name.
      'direction' => 'ASC', // Change this to 'DESC' for descending order.
      // Add more sorting criteria if needed.
    ),
    // Additional sorting criteria can be added here.
  );

  return $sorting_order;
}

Replace 'your_view_name' and 'your_display_name' with the actual name of your View and its display. Adjust 'your_field_name' with the field you want to sort by or replace it with the computed value you want to base the sorting on.

The your_custom_sorting_formula() function should contain your logic to determine the sorting order, returning an array that defines the sorting criteria based on your formula.

This approach allows for dynamic sorting based on any custom logic or formula you need within your Drupal View. Adjust the function your_custom_sorting_formula() as per your specific requirements for sorting.

ressa’s picture

This looks odd. @shrikant.dhotre is both asking and answering ...

pub497’s picture

the bots are taking over, that 100% looks like a chatGPT answer haha

shrikant.dhotre’s picture

But still not resolved still looking for answer.

pub497’s picture

hey @shrikant.dhotre I'm a bit confused what you are trying to do, are you trying to prefix the values in 'field_static_id_value' with a custom number so that sorting uses the number instead of the text?