Read More
Read More
Menu Close

    PrestaShop data provider and handler

    In this blog we are going to discuss about saving data submitted from Symfony form, in PrestaShop 1.7.8.0 . Here we are going to demonstrate PrestaShop 1.7.8.0, data provider and handler to save submitted data.

    We are continuing this blog where we end the last one.

    Step 7 :

    Add below lines in services.yml

    services:
      _defaults:
        public: true
    
      prestashop.module.demoforms.form.type.demo_first_text:
        class: 'PrestaShop\Module\DemoForms\Form\DemoFirstTextType'
        parent: 'form.type.translatable.aware'
        public: true
        tags:
          - { name: form.type }
    
      prestashop.module.demoforms.form.demo_first_text_form_data_provider:
        class: 'PrestaShop\Module\DemoForms\Form\DemoFirstTextFormDataProvider'
        arguments:
          - '@prestashop.module.demoforms.form.demo_first_text_data_configuration'
    
      prestashop.module.demoforms.form.demo_first_text_form_data_handler:
        class: 'PrestaShop\PrestaShop\Core\Form\Handler'
        arguments:
          - '@form.factory'
          - '@prestashop.core.hook.dispatcher'
          - '@prestashop.module.demoforms.form.demo_first_text_form_data_provider'
          - 'PrestaShop\Module\DemoForms\Form\DemoFirstTextType'
          - 'DemoFirst'
    
      prestashop.module.demoforms.form.demo_first_text_data_configuration:
        class: PrestaShop\Module\DemoForms\Form\DemoFirstTextDataConfiguration
        arguments: ['@prestashop.adapter.legacy.configuration']

    Services loads all the handler and data provider written in src/Form folder

    Step 8 :

    Add below line in DemoFirstController.php index function :

    Searching for an experienced
    Prestashop Company ?
    Read More
    public function index(Request $request): Response
    {
        $textFormDataHandler = $this->get(
            'prestashop.module.demoforms.form.demo_first_text_form_data_handler'
        );
        $textForm = $textFormDataHandler->getForm();
        $textForm->handleRequest($request);
        if ($textForm->isSubmitted() && $textForm->isValid()) {
            $errors = $textFormDataHandler->save($textForm->getData());
            if (empty($errors)) {
                $this->addFlash('success', 
                    $this->trans('Successful update.', 'Admin.Notifications.Success')
                );
                return $this->redirectToRoute('demo_first_form);
            }
            $this->flashErrors($errors);
       }
       return $this->render(
           '@Modules/demoforms/views/templates/admin/form.html.twig', 
           ['demoForms' => $textForm->createView(),]
       );
    }

    Here we added flash message to show on error and success of saving process using form handler.

    Symfony form data provider, handler in Prestashop 1.7.8.0
    Symfony form in PrestaShop 1.7.8.0

    Step : 9

    Add below lines in DemoFirstTextFormDataProvider.php in Form

    namespace PrestaShop\Module\DemoForms\Form;
    use PrestaShop\PrestaShop\Core\Configuration\DataConfigurationInterface;
    use PrestaShop\PrestaShop\Core\Form\FormDataProviderInterface;
    
    class DemoFirstTextFormDataProvider implements FormDataProviderInterface
    {
        private $demoFirstTextDataConfiguration;
    
        public function __construct(DataConfigurationInterface $demoFirstTextDataConfiguration)
        {
            $this->demoFirstTextDataConfiguration = $demoFirstTextDataConfiguration;
        }
    
        public function getData(): array
        {
            return $this->demoFirstTextDataConfiguration->getConfiguration();
        }
    
        public function setData(array $data): array
        {
            return $this->demoFirstTextDataConfiguration->updateConfiguration($data);
        }
    }

    Data provider class help to load saved data from class configuration as written above.

    Step 10 :

    Add below line in DemoFirstTextDataConfiguration.php in From

    namespace PrestaShop\Module\DemoForms\Form;
    use PrestaShop\PrestaShop\Core\Configuration\DataConfigurationInterface;
    use PrestaShop\PrestaShop\Core\ConfigurationInterface;
    
    final class DemoFirstTextDataConfiguration implements DataConfigurationInterface
    {    
        public const TRANSLATABLE_TEXT_AREA = 'DEMO_FORMS_TRANSLATABLE_TEXT_AREA_TYPE';
        public const FORMATTED_TEXT_AREA_TYPE = 'DEMO_FORMS_FORMATTED_TEXT_AREA_TYPE';
        private $configuration;
        public function __construct(ConfigurationInterface $configuration)
        {
            $this->configuration = $configuration;
        }
        public function getConfiguration(): array
        {
            $return = [];
            if ($translatableTextArea = $this->configuration->get(static::TRANSLATABLE_TEXT_AREA)) {
                $return['translatable_text_area_type'] = $translatableTextArea;
            }
            if ($formattedTextAreaType = $this->configuration->get(static::FORMATTED_TEXT_AREA_TYPE)) {
                $return['formatted_text_area_type'] = $formattedTextAreaType;
            }
            return $return;
        }
        public function updateConfiguration(array $configuration): array
        {
            $this->configuration->set(
                static::TRANSLATABLE_TEXT_AREA, 
                $configuration['translatable_text_area_type']
            );        
            $this->configuration->set(
                static::FORMATTED_TEXT_AREA_TYPE, $configuration['formatted_text_area_type'], 
                null,
                ['html' => true]
            );        
            return [];
        }    
        public function validateConfiguration(array $configuration): bool
        {
            return true;
        }
    }

    That’s all about PrestaShop data provider and handler, this is how you can use Symfony Form in Prestashop 1.7.8.0 to saving data in configuration class.

    If you are facing any issues or doubts in the above process, please feel free to contact us through the comment section.

    I would be happy to help.

    Also, you can explore our PrestaShop Development Services and 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