Reading list Switch to dark mode

    Adding New field in PrestaShop 1.7 Admin Symfony Controller

    Updated 8 September 2021

    In this blog, we are going to learn how to modify existing Symfony controller forms in PrestaShop.

    In PrestaShop 1.7.x.x, the system has changed as we now rely on the Symfony Form component and can modify existing forms without template override.

    So let’s understand how to add new fields and modify/remove existing fields. For this purpose we’ll use action<ClassName>FormBuilderModifier hook.

    Suppose, we want to modify the customer form then we have to use the actionCustomerFormBuilderModifier hook.

    Let’s be practical…

    Searching for an experienced
    Prestashop Company ?
    Find out More

    1) Modify and remove existing fields

    Here we’ll remove the First name field and will modify the Last name field’s of the customer form

    Current view of the form:

    Modify Existing Symfony Controller Form In PrestaShop With A Module Image 1

    In the hook function, this is the example code 

    public function hookActionCustomerFormBuilderModifier($params)
    {
         /** @var $formBuilder \Symfony\Component\Form */
         $formBuilder = $params['form_builder'];
         $formBuilder->remove('first_name');
         $formBuilder->get('last_name')->setRequired(false);
     }

    As we can see, the First name has been removed and the Last name field has been modified to a non-required field.

    Modify Existing Symfony Controller Form In PrestaShop With A Module Image 2

    2) Add a new field

    Now, we’ll add Mobile Number Field just after the Email field

    public function hookActionCustomerFormBuilderModifier($params)
    {
        /** @var $formBuilder \Symfony\Component\Form */
        $formBuilder = $params['form_builder'];
        $formBuilder->remove('first_name');
        $formBuilder->get('last_name')->setRequired(false);
        $allFields = $formBuilder->all();
        foreach ($allFields as $inputField => $input) {
            $formBuilder->remove($inputField);
        }
        foreach ($allFields as $inputField => $input) {
            $formBuilder->add($input);
            if ($inputField == 'email') {
                /** @var TextType::class \Symfony\Component\Form\Extension\Core\Type\TextType */
                $formBuilder->add(
                    'mobile_number', 
                     TextType::class, 
                     ['label' => 'Mobile Number']
                );
            }
         }
      }
    Modify Existing Symfony Controller Form In PrestaShop With A Module Image 3

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

    Methods of formBuilder class can be seen from these two files

    i)<prestashop_root_directory>/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilderInterface.php

    ii)<prestashop_root_directory>/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php

    Note: 

    1) Here, we have modified only the view of the form. To save data of new fields, use these hooks

    i) actionObject<ClassName>AddAfter

    ii) actionObject<ClassName>UpdateAfter

    2) If action<ClassName>FormBuilderModifier hook is not supported in some PrestaShop’s versions then the same functionality can be achieved by template override.

    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].

    . . .

    Leave a Comment

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


    Be the first to comment.

    Back to Top

    Message Sent!

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

    Back to Home