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 selecting 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 services page will trigger an hook called onBeforeListQueryServices.

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_vikappointments/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 appointments by service 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 onBeforeListQueryReservations(&$query, $view)
{
    // get service ID from request or from the user state
    $app        = JFactory::getApplication();
    $id_service = $app->getUserStateFromRequest(
        // get cache signature to keep the filter set in request
        $view->getPoolName() . '.service',
        // get key of the filter set in request
        'id_service',
        // define a default value when the filter is missing
        0,
        // set the type of the filter
        'uint'
    );

    if ($id_service)
    {
        // extend the query with a new condition
        $query->where($dbo->qn('r.id_service') . ' = ' . $id_service);
    }
}

Changelog

VersionDescription
1.6.2 Introduced.
Last Update: 2021-10-10 10:19
Helpful?