public function onBeforeListQuery{$page}( mixed &$query, JView $view ) : void

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

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:

/administrator/components/com_vikrestaurants/views/{$page}/view.html.php
(back-end pages) 

Parameters

&$query

(mixed)  Either a query string or a query builder instance.

$page

(JView)  The page instance.

Return Value

None.


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   mixed  &$query  The query string or a query builder object.
 * @param   mixed  $view    The current view instance.
 *
 * @return  void
 */
public function onBeforeListQueryTkreservations(&$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);
    }
}

Changelog

Version Description
1.8 Introduced.
Last Update: 2023-12-29 14:15
Helpful?