Skip to main content

Add Custom Functionality to Submission

Let's create observer function for WebForms that dumps submitted result into XML file.

We will be creating new extension called WebFormsProcessResult.

File structure

app/code/Mageme/WebFormsProcessResult
├── etc
│ ├── events.xml
│ └── module.xml
├── Observer
│ └── WebformsResultSaveObserver.php
├── composer.json
└── registration.php

composer.json

This file holds information for composer script

{
"name": "mageme/webformsprocessresult",
"description": "",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0",
"vladimirpopov/webforms": "2.8.2",

"magento/magento-composer-installer": "*"
},
"suggest": {

},
"type": "magento2-module",
"version": "0.1.0",
"license": [

],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Mageme\\WebFormsProcessResult\\": ""
}
},
"extra": {
"map": [
[
"*",
"Mageme/WebFormsProcessResult"
]
]
}
}

registration.php

This file registers module in Magento system. Without this file the module is not visible.

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Mageme_WebFormsProcessResult',
__DIR__
);

etc/module.xml

This file holds basic description of our module. We need to set the sequence so that the module is loaded after the main WebForms extension.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mageme_WebFormsProcessResult" setup_version="0.1.0">
<sequence>
<module name="VladimirPopov_WebForms"/>
</sequence>
</module>
</config>

etc/events.xml

This file registers our event handler function.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="webforms_result_save">
<observer name="mageme_webformsprocessresult_webforms_result_save"
instance="Mageme\WebFormsProcessResult\Observer\WebformsResultSaveObserver" />
</event>
</config>

Observer/WebformsResultSaveObserver.php

Here we define our model of the event observer class. It has some basic members to work with stores, system messages and logging.

<?php
namespace Mageme\WebFormsProcessResult\Observer;

use Magento\Framework\Event\ObserverInterface;
use VladimirPopov\WebForms\Model;

class WebformsResultSaveObserver implements ObserverInterface
{
/** @var \Magento\Store\Model\StoreManager */
protected $_storeManager;

/** @var \Psr\Log\LoggerInterface */
protected $logger;

/**
* @var \Magento\Framework\Message\ManagerInterface
*/
protected $messageManager;

public function __construct(
\Magento\Store\Model\StoreManager $storeManager,
\Magento\Framework\Message\ManagerInterface $messageManager,
\Psr\Log\LoggerInterface $logger
)
{
$this->_storeManager = $storeManager;
$this->messageManager = $messageManager;
$this->logger = $logger;
}

/**
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{

/** @var Model\Form $webform */
$webform = $observer->getWebform();

/*
* Check web-form code
* if($webform->getCode() != 'myform') return $this;
*/

/** @var Model\Result $result */
$result = $observer->getResult();

$xmlObject = new \Magento\Framework\Simplexml\Element($result->toXml());

// generate unique filename
$mediaFolder = $this->_storeManager->getStore($result->getStoreId())->getBaseMediaDir();
$destinationFolder = $mediaFolder . '/' . 'webforms' . '/' . 'xml';
$filename = $destinationFolder . '/' . $result->getId() . '.xml';

// create folder
if (!(@is_dir($destinationFolder) || @mkdir($destinationFolder, 0755, true))) {
$this->messageManager->addErrorMessage("Unable to create directory '{$destinationFolder}'.");
} else {

// export to file
$xmlObject->asNiceXml($filename);
}
return $this;
}
}

$destinationFolder - variable that sets folder where result is being stored. We will be creating files in pub/media/webforms/xml folder.

$filename - is complete path to the file. For example pub/media/webforms/xml/123.xml.

note

Make sure pub/media folder is writable.

After the module is ready, please run following console commands:

php bin/magento module:enable Mageme_WebFormsProcessResult
php bin/magento setup:upgrade

Thats it! Feel free to modify this extension, experiment and add required post processing functionality to WebForms.

You can download sources here.