Request Quote

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

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?

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>';
        }
    }
}

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

. . .

Comments (7)

Add Your Comment

  • Bruce
    Thanks for your reply,
    In my callback method, i try to implement it but it’s doesn’t work…
    My code :
    if (!isset($params[‘fields’][‘latest_ship_date’])) {
    //$params[‘fields’][‘latest_ship_date’] = array(

    $latest_ship_date = array( ‘latest_ship_date’ =>
    array(
    ‘title’ => $this->l(‘Date Exp. Max.’),
    ‘align’ => ‘text-right’,
    “filter_key” => “amo!latest_ship_date”,
    “callback” => ‘LastShip’,
    ‘type’ => ‘datetime’,
    “callback_object” => Module::getInstanceByName($this->name),
    //”class” => $classe,
    )
    );
    $this->addNewFieldAfter($latest_ship_date, ‘date_add’, $params[‘fields’]);
    }
    public function LastShip($value)
    {
    $value = date_format($value, ‘d/m/Y H:i:s’);
    //$value = strtotime($value);
    $datenow = date(“d/m/Y H:i:s”);
    if ($value < $datenow)
    {
    return '’.$value.”;
    }
    }
    Thank you.
    Bruce

    • Sanjay Singh
      You need to add the array of your field list in the passed parameter of the method.
      Replace if (!isset($params[‘fields’][‘latest_ship_date’])) with
      if (isset($params[‘fields’][‘latest_ship_date’]))
      and set the $params[‘fields’][‘latest_ship_date’] = array(
      ‘title’ => $this->l(‘Date Exp. Max.’),
      ‘align’ => ‘text-right’,
      “filter_key” => “amo!latest_ship_date”,
      “callback” => ‘LastShip’,
      ‘type’ => ‘datetime’,
      “callback_object” => Module::getInstanceByName($this->name),
      //”class” => $classe,
      )
      • Bruce
        This is my function
        public function LastShip($value)
        {
        //$DateMaxExp = date_format($value, “d/m/Y”);
        $DateNow = date(“d/m/Y”);

        if ($value < $DateNow)
        {

        //return $DateMaxExp;
        $classe = 'bold';
        //return $value;
        //return $tr['latest_ship_date'];
        }
        }
        But $classe is not returned…

        • Sanjay Singh
          You need to return the value from the callback method to display the column value in the render list
          As in the callback method, we get 2 params
          1) cell value
          2) The entire row detail
          From the callback method, you need to return the value to be displayed in the list

          public function LastShip($value)
          {
          $DateNow = date(“d/m/Y”);
          if ($value < $DateNow)
          {
          $classe = 'bold';
          return $classe;
          }
          return '–';
          }

          • Bruce
            Thanks a lot !
  • Bruce
    Hello,
    I would like to apply a class if my field (datetime) is < date now.
    Thanks a lot.
    Bruce
    • Sanjay Singh
      Hello,
      You can check the value of your field (DateTime) in the callback method and if it satisfies your condition then you can add your class accordingly. Hope it will help.
  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close