Reading list Switch to dark mode

    Callback Method to Add Custom Content On Admin Core Controller RenderList In Prestashop

    Updated 16 July 2021

    Many time we need to show some custom content on Admin Core Controller RenderList.

    To display the custom field in the admin controller render list. We need to register the action{ControllerName}ListingFieldsModifier hook.

    Check this link https://webkul.com/blog/how-to-modify-fields-list-in-prestashop/  to display the custom field in render list.

    To display own HTML content or any customize data, we can use the callback parameter of renderList. But callback searches the method in its own controller.  We cannot add the method on admin core controller.

    How are we going to use callback inside action{ControllerName}ListingFieldsModifier in our module?

    Start your headless eCommerce
    now.
    Find out More

    So first we will understand the callback flow and how we can use it in our module.

    if (isset($params['callback'])) {
        $callback_obj= (isset($params['callback_object'])) ? $params['callback_object'] : $this->context->controller;
        if (!preg_match('/<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)/ism', call_user_func_array(array($callback_obj, $params['callback']), array($field_value, $row)))) {
            $field_value=call_user_func_array(array($callback_obj, $params['callback']), array($field_value, $row));
        }
    }

    Prestashop admin controller first checks the callback parameter is set or not. If set then it checks the callback_object parameter.

    If we set the callback_object then the callback method will be searched inside the set object. Otherwise, it will search the callback method in the admin controller.

    So how we are going to add our callback method?

    So for adding our callback method, we are having a callback_object parameter in the params object.

    In callback_object parameter, we will pass the object of the Module

    class CallbackExample extends Module
    {
        ...
        public function hookActionAdminOrdersListingFieldsModifier($list)
        {
            $optionsOrderStatus = array(1 => 'Select');
            if (isset($list['select'])) {
                $list['select'] .= ', o.`status` AS `type`';
            }
            $list['fields']['type'] = array(
                'title' => 'Type',
                'align' => 'text-center',
                'filter_key' => 'o!status',
                'callback' => 'callbackMethod',
                'orderby' => false,
                'type' => 'select',
                'list' => $optionsOrderStatus,
                'callback_object' => Module::getInstanceByName($this->name)
            );
        }
        public function callbackMethod($value)
        {
            if ($value) {
                return '<span class="label label-primary">Test Label</span>';
            }
        }
    }
    
    image

    So this is how we are going to add our field in the render list of admin controller with our callback method.

    . . .

    Leave a Comment

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


    9 comments

  • Fred
    • Sanjay Singh (Moderator)
  • Bruce
    • Sanjay Singh (Moderator)
      • Bruce
        • Sanjay Singh (Moderator)
          • Bruce
  • Bruce
    • Sanjay Singh (Moderator)
  • Back to Top

    Message Sent!

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

    Back to Home