public function onDisplayView{$page}List( JView $view, bool &$searching, array $config ) : array

Fires while displaying the list page of a specific entity.


Description

This hook is triggered within the list pages of certain sections of the plugin. It is possible to use this hook to include additional pieces of code within specific positions of the related page, such as to extend the filters bar.

The dynamic portion of the hook name, $page, refers to the name of the page calling the hook. This means that, for example, the take-away orders page will trigger an hook called onDisplayViewTkreservationsList.

IMPORTANT NOTE: not all the pages trigger this hook. Before to start coding a plugin, you should make sure that the page supports that hook, by checking whether the onDisplayListView() method is used. This can be done by accessing the page at the path:

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

Parameters

$page

(JView)  The page instance holding the record details.

&$searching

(bool)  True whether any custom filters are set in request. When this flag is set to true, the "Search Tools" button will be active.

$config

(array)  An associative array of options.

Return Value

Array. An associative array containing the HTML to include. By adding the HTML within a reserved key of the page, the plugin will display the entered string within the position assigned to that key. Here's a list of supported keys:

  • search - append HTML within the search bar;
  • filters - append HTML within the filters bar (the one under the "Search Tools" button).

Example

The example below displays a field to filter the take-away orders by payment.

/**
 * Trigger filter to allow the plugins to include custom HTML within the page. 
 *
 * @param   mixed  $view        The current page instance.
 * @param   bool   &$searching  Set it to TRUE to open the Search Tools.
 * @param   array  $config      A configuration array.
 *
 * @return  array  An associative array of forms.
 */
public function onDisplayViewTkreservationsList($view, &$searching, $config)
{
    $forms = [];

    // 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
        '',
        // set the type of the filter
        'uint'
    );

    if ($id_payment) {
        // filter was set, activate Search Tools button
        $searching = true;
    }

    // create list of available payment gateways
    $options = JHtml::_('vrehtml.admin.payments', 'takeaway');

    // convert options into HTML
    $options = JHtml::_('select.options', $options, 'value', 'text', $id_payment);

    // create select element
    $select = sprintf(
        '<select name="id_payment" id="vap-payment-sel" class="%s">%s</select>',
        $id_payment ? 'active' : '',
        $options
    );

    // create container, aligned to left
    $html = '<div class="btn-group pull-left">' . $select . '</div>';

    // add script to support interactions
    JFactory::getDocument()->addScriptDeclaration("
(function($) {
    'use strict';

    $(function() {
        // auto-submit form when payment filter changes
        $('select[name=\"id_payment\"]').on('change', () => {
            document.adminForm.submit();
        });

        // take method used to clear the filters
        const __clearFilters = clearFilters;
        // override method to unset custom filters
        // when the clear button gets clicked
        clearFilters = () => {
            // unset payment field
            $('select[name=\"id_payment\"]').updateChosen('');
            // invoke parent to clear default fields
            __clearFilters();
        }
    });
})(Query);
    ");

    // add field at the end of the filters section
    $forms['filters'] = $html;

    return $forms;
}

Changelog

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