Before List Query
apply_filters_ref_array( 'vikrestaurants_before_list_query_{$page}', bool $status, mixed &$query, JView $view )
Fires before executing the main query of a list page.
Description
This hook is triggered before executing the query used to retrieve a list of records. It is possible to manipulate here the query in order to change its default functionalities, for example by including more columns and tables or by adding/removing certain restrictions.
The dynamic portion of the hook name, $page
, refers to the name of the page executing the query. This means that, for example, the reservations page will trigger an hook called vikrestaurants_before_list_query_reservations
.
IMPORTANT NOTE: not all the pages trigger this hook. Before to start coding a plugin, you should make sure that the query of the page supports that hook, by checking whether the onBeforeListQuery()
method is used. This can be done by accessing the page at the path:
/wp-content/plugins/vikrestaurants/admin/views/{$page}/view.html.php
(back-end pages)
Parameters
- $status
-
(bool) True on success, false otherwise.
- &$query
-
(mixed) Either a query string or a query builder instance.
- $page
-
(JView) The page instance.
Example
The example below includes within the query the possibility of filtering the take-away orders by payment ID, which it is assumed to be set inside the request.
/**
* Trigger hook to allow the plugins to manipulate the query used to retrieve
* a standard list of records.
*
* @param bool $status True on success, false otherwise.
* @param mixed &$query The query string or a query builder object.
* @param mixed $view The current view instance.
*/
add_filter('vikrestaurants_before_list_query_tkreservations', function($status, &$query, $view) {
// get payment ID from request or from the user state
$app = JFactory::getApplication();
$id_payment = $app->getUserStateFromRequest(
// get cache signature to keep the filter set in request
$view->getPoolName() . '.payment',
// get key of the filter set in request
'id_payment',
// define a default value when the filter is missing
0,
// set the type of the filter
'uint'
);
if ($id_payment) {
// extend the query with a new condition
$query->where('r.id_payment = ' . $id_payment);
}
return $status;
}, 10, 3);
Changelog
Version | Description |
---|---|
1.0 | Introduced. |