Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5554

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

$
0
0

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

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


Viewing all articles
Browse latest Browse all 5554

Trending Articles