[DACHCOM-Formbuilder] save formdata as dataobject

Is it possible to define an Objectclass in dachcom/Formbuilder and save formdata to object when event FormBuilderEvents::FORM_SUBMIT_SUCCESS trigger ?

Andreas

1 Like

Yes, this is what I do:

app/config/services.yml :

    AppBundle\EventListener\EduFormListener:
        tags:
            - { name: kernel.event_listener }

src\AppBundle\EventListener\EduFormListener.php :

<?php

namespace AppBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use FormBuilderBundle\Event\SubmissionEvent;
use FormBuilderBundle\FormBuilderEvents;
use FormBuilderBundle\Storage\FormInterface as FormBuilderFormInterface;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Customer;
use Cocur\Slugify\Slugify;

class EduFormListener implements EventSubscriberInterface
{
    /**
     * @inheritDoc
     */
    public static function getSubscribedEvents()
    {
        return [
            FormBuilderEvents::FORM_SUBMIT_SUCCESS => ['formSubmitSuccess'],
        ];
    }

    public function formSubmitSuccess(SubmissionEvent $event)
    {
        $request = $event->getRequest();
        $form = $event->getForm();
        /** @var FormBuilderFormInterface $data */
        $data = $form->getData();
        $formId = $data->getId();
        $formConfiguration = $event->getFormConfiguration();
        \Pimcore\Log\Simple::log('lovia', 'FORM SUBMIT id: ' . json_encode($formId, JSON_PRETTY_PRINT, 2));
        \Pimcore\Log\Simple::log('lovia', 'FORM SUBMIT fields: ' . json_encode($data->getFields(), JSON_PRETTY_PRINT, 2));
        $general = $data->getFieldValue('general');
        \Pimcore\Log\Simple::log('lovia', 'general=' . json_encode($general));
        //\Pimcore\Log\Simple::log('lovia', 'fullName=' . $general[0]['fullName']);

        $customer = new Customer();
        $customer->setParent(DataObject::getByPath('/customers'));
        $customer->setPublished(true);
        $suffix = '';
        for ($i = 0; $i < 6; $i++) 
        {
            $suffix .= mt_rand(0, 9);
        }
        $customer->setFirstName($general[0]['firstName']);
        $slugify = new Slugify();
        $customer->setKey($slugify->slugify($customer->getFirstName()) . $suffix);
        $customer->setLastName($general[0]['lastName']);
        $customer->setGender($general[0]['gender']);
        $customer->setBirthdate($general[0]['birthdate']);
        $customer->setEmail($general[0]['email']);
        $customer->setPhone($general[0]['phone']);
        $customer->setNewsletterActive(true);
        $customer->save();
        \Pimcore\Log\Simple::log('lovia', 'customer=' . $customer->getFullPath());
    }
}