By shrikant.dhotre on
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
Certainly! In Drupal, using hook_views_query_alter to change the
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:
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.
This looks odd. @shrikant
This looks odd. @shrikant.dhotre is both asking and answering ...
the bots are taking over,
the bots are taking over, that 100% looks like a chatGPT answer haha
Yes, Pasted ChatGPT Answer
But still not resolved still looking for answer.
hey @shrikant.dhotre I'm a
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?