Menu Close
    Searching for an experienced Prestashop Development Company ?

    Adding New Configuration Fields In PrestaShop 1.7 Modern Pages

    In this blog, we are going to learn how to add new configuration fields in existing Symfony controller forms in PrestaShop 1.7. This concept will be helpful if we want to add new fields in native PrestaShop configuration pages.

    So let’s understand how to add new fields. For this purpose we’ll use the action<layoutTitle>PageForm and  action<layoutTitle>PageSave hooks. Hence, the layoutTitle is the display name of the page, It can be seen from the header of the page.

    Suppose, if we want to add the new configuration field in the Performance page then we have to use the actionPerformancePageForm and actionPerformancePageSave hooks.

    Let’s understand it with an example…

    i) Create a module and register the hooks

    Hence we have created a demo module and registered the mentioned hook, like below code-

    public function hookActionPerformancePageForm(&$params)
    {
        # /modules/mymodule/mymodule.php
        $formBuilder = $params['form_builder'];
        /** @var $formBuilder \Symfony\Component\Form */
        $cacheLifeTime = $formBuilder->get('smarty');
        /** @var TextType::class \Symfony\Component\Form\Extension\Core\Type\TextType */
        $cacheLifeTime->add(
            'cache_life_time',
            TextType::class,
            [
                'help' => $this->l('Add cache lifetime in hrs'),
                'label' => $this->l('Cache lifetime')
            ]
        );
    }
    

    In the above code, we get the smarty section of the Performance page using the get method and add a new text type field “Cache lifetime”

    Hence, the text type field is created so we need to use the namespace Symfony\Component\Form\Extension\Core\Type\TextType;  in the module.

    Now, the view of the form is like this :

    Adding New Configuration Fields In PrestaShop 1.7 Modern Pages Image 1

    ii) Validating/Saving forms data
    For validation and Saving the form data, we will use hook actionPerformancePageSave as mentioned below-

    public function hookActionPerformancePageSave(&$params)
    {
        # /modules/mymodule/mymodule.php
        // retrieve and validate the data
        $cacheLifeTime = $params['form_data']['smarty']['cache_life_time'];
        if (!Validate::isFloat($cacheLifeTime)) {
            $params['errors'] = [$this->l('Cache Lifetime is not valid')];
        }
        if (empty($params['errors'])) {
            Configuration::updateValue('PS_CACHE_LIFE_TIME', $cacheLifeTime);
        }
     }
    

    We can add errors in $params[‘errors’] in the form of an array and PrestaShop will display these errors automatically. If data is valid then save this data in the configuration table, hence we have saved it in the PS_CACHE_LIFE_TIME configuration key.

    Errors will be shown like given screenshot-

    adding-new-configuration-fields-in-prestashop-1-7-modern-pages-image-3

    iii) Display updated data in fields

    Add the ‘data’ parameter just like the below code.

    public function hookActionPerformancePageForm(&$params)
    {
        # /modules/mymodule/mymodule.php
        $formBuilder = $params['form_builder'];
        /** @var $formBuilder \Symfony\Component\Form */
        $cacheLifeTime = $formBuilder->get('smarty');
        /** @var TextType::class \Symfony\Component\Form\Extension\Core\Type\TextType */
        $cacheLifeTime->add(
            'cache_life_time',
            TextType::class,
            [
               'help' => $this->l('Add cache lifetime in hrs'),
               'label' => $this->l('Cache lifetime'),
               'data' => Configuration::get('PS_CACHE_LIFE_TIME')
            ]
        );
     }
    

    Now updated value will be shown like this:-

    adding-new-configuration-fields-in-prestashop-1-7-modern-pages-image-4

    Note: This concept only works with pages from the “Configure” section of your back office.

    That’s all about this blog.

    If any issue or doubt please feel free to mention it in the comment section.

    We would be happy to help.

    Also, you can explore our PrestaShop Development Services & a large range of quality PrestaShop Modules.

    For any doubt contact us at [email protected].

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    Be the first to comment.

    Back to Top