Italian (Italy)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  System  >  Visualization  >  Display List Page

apply_filters_ref_array( 'vikrestaurants_display_view_{$page}_list', mixed $forms, JView $view, bool &$searching, array $config )

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

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:

/wp-content/plugins/vikrestaurants/admin/views/{$page}/tmpl/default.php
(back-end pages)

Parameters

$forms

(array|null)  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).

At the first hook execution, the argument may be a null value.

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


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  $forms       An associative array of forms.
 * @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.
 */
add_filter('vikrestaurants_display_view_tkreservations_list', function($forms, $view, &$searching, $config) {
    if (!$forms) {
        $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
    add_action('admin_print_footer_scripts', function() {
        ?>
<script>
    (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();
            }
        });
    })(jQuery);
</script>
        <?php
    });

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

    return $forms;
}, 10, 4);

Changelog

Version Description
1.3 Introduced.
Ultimo aggiornamento: 2024-01-04
Utile?