Read More
Read More
Menu Close

    Adding a custom grid column type in PrestaShop admin symfony controller with module

    PrestaShop provide these defult type of column which we can use when we add a new column in PrestaShop admin controller list.

    Refer to this blog to see how to create new column in admin symfony controller

    Now the requirement is how we can create our custom column type in our module according to our requirement. For example: Prestashop does not provide HTML type column type. Let see how you create this.

    In your module create folder like this and create a class file

    yourmodule/src/Grid/Column/HtmlTypeColumn.php

    Start your headless eCommerce
    now.
    Read More

    In this HtmlTypeColumn.php file the code is,

    namespace MyModule\Grid\Column;
    
    use PrestaShop\PrestaShop\Core\Grid\Column\AbstractColumn;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    
    final class HtmlTypeColumn extends AbstractColumn
    {
        /**
         * {@inheritdoc}
         */
        public function getType()
        {
            return 'mymodule_button';
        }
    
        /**
         * {@inheritdoc}
         */
        protected function configureOptions(OptionsResolver $resolver)
        {
            $resolver
                ->setRequired([
                    'ModuleClass',
                    'custom_text'
                ])
                ->setAllowedTypes('ModuleClass', 'object')
                ->setAllowedTypes('custom_text', 'string');
        }
    }

    You can use this class now in ‘actionCustomerGridDefinitionModifier‘ hook to create column in prestashop controller OR in your own new controller

        public function hookActionCustomerGridDefinitionModifier(array $params)
        {
            /** @var GridDefinitionInterface $definition */
            $definition = $params['definition'];
    
            $definition
                ->getColumns()
                ->addAfter(
                    'optin',
                    (new MyModule\Grid\Column\HtmlTypeColumn('wk_button'))
                        ->setName($this->l('My Custom Button'))
                        ->setOptions([
                            'ModuleClass' => new MyModule(),
                            'custom_text' => $this->l('Click here')
                        ])
                )
            ;
        }

    Now add your html code to create this HTMLType with folder

    yourmodule/views/PrestaShop/Admin/Common/Grid/Columns/Content/mymodule_button.html.twig

    make sure the twig file name should match with getType().

    {% set login_link = column.options.ModuleClass.getLink(record['id_customer']) %}
    <a href="{{ login_link  }}"
    target="_blank" class="btn btn-primary pointer">
        <i class="icon-user"></i> {{ column.options.custom_text }}
    </a>

    Done. Now you can see the button in the list where you add this HTMLType. Like here we added on the customer list page.

    Note: As we have created class in src/ folder so make sure you have autoload this with this command:

    $ composer dumpautoload

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    5 comments

  • Martynas
    can you share module, i keep getting “Attempted to load class” error
    made composer dumpautoload but still same problem
  • Massimiliano Palermo
    Hi, i tried to use your suggestions creating a custom row in order list. My module is called MpBrtInfoSoap, so i created cuestom button mpbrtinfosoap_brtbutton type and inserted twig code in mpbrtinfosoap/views/Prestashop/Admin/Common/Grid/Columns/Content/mpbrtinfosoap_brtbutton.html.twig

    Here’s a part of column definition class

    final class HtmlTypeColumn extends AbstractColumn
    {
    /**
    * {@inheritdoc}
    */
    public function getType()
    {
    return ‘mpbrtinfosoap_brtbutton’;
    }

    When Prestashop renders orders list, throws this exception:

    An exception has been thrown during the rendering of a template (“Content template for column type “mpbrtinfosoap_brtbutton” was not found”).

    Can you help me to resolve this issue?

    Thanks, Massimiliano.

    • amit kushwaha (Moderator)
      Hello,
      Greetings from Webkul!
      Thank you for your comment, I would request you to please raise a ticket at http://support.webkul.com/ so we can assist you regarding your mentioned issue.

      Best Regards.

  • Tom
    Hello, thx for this tutorial 🙂

    Is ther any way how to make this work without using composer dumpautoload? I dont have chance to use it its look like my custom htmltypecolumn is not loaded.

    Thx for any help 🙂

    • Dheeraj Sharma (Moderator)
      Hello, you need to use this 
      $ composer dumpautoload
      to create the autoload file for your src folder, its required other way you can create link manually. 
      It will load after this command.
      Thanks,
  • Back to Top