Back to Top

How to modify fields list before rendring of an Admin controller in prestashop

Updated 6 May 2020

In this blog we are going to learn how to modify fields list  before rendring of an Admin controller in prestashop.

As we all know in an admin controller to show a render list, it is must to create a fields list. We define the $fields_list variable in the admin controller. In the $fields_list variable, we define all the required things in the render list.

All the columns defined in the $fields_list variable are shown in the render list.

Let’s understand the entire process with the help of taking an example of  AdminAddressesController. In the below code you can see how fields list is defined in the AdminAddressesController-

$this->fields_list = array(
    'id_address' => array(
        'title' => $this->l('ID'),
        'align' => 'center',
        'class' => 'fixed-width-xs'
        ),
    'firstname' => array(
        'title' => $this->l('First Name'),
        'filter_key' => 'a!firstname'),
    'lastname' => array(
        'title' => $this->l('Last Name'), 
        'filter_key' => 'a!lastname'
        ),
    'address1' => array(
        'title' => $this->l('Address')
        ),
    'postcode' => array(
        'title' => $this->l('Zip/Postal Code'), 
        'align' => 'right'
        ),
    'city' => array(
        'title' => $this->l('City')
        ),
    'country' => array(
        'title' => $this->l('Country'), 
        'type' => 'select', 
        'list' => $this->countries_array, 
        'filter_key' => 'cl!id_country'
        ), 
    );

 And renderlist made by the above fields list in AdminAddressesController looks like the below image-

Searching for an experienced
Prestashop Company ?
Find out More

AdminAddressesController render list

It is the renderlist rendered by the predefined $fields_list variable in the AdminAddressesController. Now what if you want to add some more columns in this renderlist.

Prestashop provides a hook in the AdminController.php class through which we can modify the coulums of the renderlist of any admin controller.

//Hook provided in the getList() method in AdminController.php
Hook::exec('action'.$this->controller_name.'ListingFieldsModifier', array(
    'select' => &$this->_select,
    'join' => &$this->_join,
    'where' => &$this->_where,
    'group_by' => &$this->_group,
    'order_by' => &$this->_orderBy,
    'order_way' => &$this->_orderWay,
    'fields' => &$this->fields_list,
));

Note :: In the hook $this->controller is the name of the admin controller which fields list you are going to change.

Using the above hook you can modify the $fields_list of the admin controller’s render list. Lets understand the process with an example-

We are going to add a column in the AdminAddressesController’s render list which will show the email address of the customer. What you have to do is in your main class of your module you have to define the above hook. Example-

/*
* 2007-2016 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <[email protected]>
*  @copyright  2007-2016 PrestaShop SA
*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

public function hookActionAdminAddressesListingFieldsModifier($params)
{
    if (isset($params['select'])) {
        $params['join'] .= ' LEFT JOIN '._DB_PREFIX_.'customer cst ON (a.id_customer = cst.id_customer)';
        $params['select'] .= ', cst.email as customer_email';
    }
    $params['fields']['customer_email'] = array(
        'title' => 'Email',
        'align' => 'center',
        'search' => true,
        'havingFilter' => true,
    );
}

In the above example we have replaced $this->controller with the admin controller’s name which render list we are going to modify i.e. AdminAddresses . 

We just have to use the parameters provided in the hook wisely and we can modify the renderlist of the backoffice controller as per our requirements.

In the above implementation, we have added the email of the customer from _DB_PREFIX_.’customer’ table in the $fields_list of the AdminAddresses controller and you can see in the below image how it is added in the renderlist of the controller-

AdminAddressesController modified render list

So this is how you can modify fields list before rendering of an Admin controller in PrestaShop.

. . .

Leave a Comment

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


11 comments

  • faf mauro
  • Avi
    • Sumit (Moderator)
      • Onklmaps
        • Sumit (Moderator)
  • Jose Fanjul
  • karlix
    • Sumit (Moderator)
  • Qasim Zubair
    • sumit
  • Qasim Zubair
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home