Menu Close
    Searching for an experienced Prestashop Development Company ?

    How to Customize renderlist of a controller in Backoffice

    We generally create a  renderlist in our controller to show saved data in a table and renderlist shows all the rows saved in the table defined as $this->table = ‘table_name’. Whichever fields we have to show in the renderlist we just have to mention those table fields with the required predefined parameters in the $this->fields_list variable.

    But in some case we have to customize the list eg.-

    Case 1 –  If we want some extra fields in the list which do not belong to the table defined.

    Case 2 –  If we do not want our list creates from a table but we just want to create it from a desired array.

    Lets start learning how to execute both the above cases-

    Prestashop provides a Hook which we are going to use for the above tasks. Lets understand the hook first.

    Hook::exec('action'.$this->controller_name.'ListingResultsModifier', array(
    	'list' => &$this->_list,
    	'list_total' => &$this->_listTotal,
    ));

    In this hook “$this->controller_name” is name of the controller in which you are going to show the customized render list.

    See the value of the parameters of the hook. It provides you the address value of the array element means the list array and the total number of list element.

    Now For Case 1, if you want to add your fields in the list array of the render list then you just have to add your desired elements in the list array of the hook’s parameters array. e.g. –

    public function hookActionAdminMyControllerListingResultsModifier($params)
    {
        //If you want to customize the renderlist by adding some of your desired elements in the list
        foreach ($params['list'] as $key => $list_row) {
            $params['list'][$key]['my_element_1'] = 'value_element_1'; //add your first element in the row of the renderlist here
            $params['list'][$key]['my_element_2'] = 'value_element_2'; //add your second element in the row of the renderlist here
        }
    }

    Now in the controller you can use the fields of the table as well as the added fields by you in the fields_list array. e.g.-

    $this->bootstrap = true;
    $this->table = 'customer_info';
    $this->fields_list = array(
        // element belongs to the above defined table
        'id' => array(
            'title' => $this->l('Customer Id'),
            'align' => 'center',
        ),
        // element belongs to the above defined table
        'name' => array(
            'title' => $this->l('Customer Name'),
            'align' => 'center',
        ),
        // element belongs to the above defined table
        'email' => array(
            'title' => $this->l('Customer Email'),
            'align' => 'center',
        ),
        // element belongs to the above defined table
        'phone' => array(
            'title' => $this->l('Customet Contact Number'),
            'align' => 'center',
        ),
        // element1 added by you in the hook
        'my_element_1' => array(
            'title' => $this->l('My Added Element_1'),
            'align' => 'center',
        ),
        // element2 added by you in the hook
        'my_element_2' => array(
            'title' => $this->l('My Added Element_2'),
            'align' => 'center',
        ),
    );
    

    Now For Case 2, if you want your customized new list independent of any class or table then you just have to assign your array as the value of “list” element of the parameters array (Make sure the array must be in the required format of the renderlist) and assign count (MyArray) as the “list_total” value in the parameters array. e.g. –

    public function hookActionAdminMyControllerListingResultsModifier($params)
    {
        //If you want your array in the renderlist independent of any table
        
        // array should be in required format of render list. 
        $my_renderlist_array = array (
            [0] => array (
                ['customer_name'] => 'John',
                ['customer_email'] => '[email protected]',
                ['customer_phone'] => '7676898976',
                ['customer_age'] => 40,
            ),
            [1] => array (
                ['customer_name'] => 'michael',
                ['customer_email'] => '[email protected]',
                ['customer_phone'] => '8655898976',
                ['customer_age'] => 20,
            ),
            [2] => array (
                ['customer_name'] => 'Carter',
                ['customer_email'] => '[email protected]',
                ['customer_phone'] => '9986854676',
                ['customer_age'] => 29,
            ),
            [3] => array (
                ['customer_name'] => 'Jonathan',
                ['customer_email'] => '[email protected]',
                ['customer_phone'] => '88768989676',
                ['customer_age'] => 34,
            ),
        );
        
        $params['list'] = $my_renderlist_array;  // assign your array to the list parameter.
        $params['list_total'] = count($params['list']);  // assign count of your array to the number of rows of renderlist.
    }

    Now in the controller you have to define your fields_list using only the elements of your array assigned by you to the list array in the hook. e.g. –

    $this->bootstrap = true;
    //All elements belongs to the array assigned to the list parameter by you in the hook
    $this->fields_list = array(
        'customer_name' => array(
            'title' => $this->l('Customer Name'),
            'align' => 'center',
        ),
        'customer_email' => array(
            'title' => $this->l('Customer Email'),
            'align' => 'center',
        ),
        'customer_phone' => array(
            'title' => $this->l('Customet Contact Number'),
            'align' => 'center',
        ),
        'customer_age' => array(
            'title' => $this->l('Customer Age'),
            'align' => 'center',
        ),
    );
    

    There can be many other uses of this hook, which you can use according to your requirement. So that’s how we can use this hook provided by prestashop to manipulate or change the render list of your controller in the back office.

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top