Reading list Switch to dark mode

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

    Updated 11 October 2019

    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.
    Find out 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

    . . .

    Leave a Comment

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


    5 comments

  • Martynas
  • Massimiliano Palermo
    • amit kushwaha (Moderator)
  • Tom
    • Dheeraj Sharma (Moderator)
  • Back to Top

    Message Sent!

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

    Back to Home