Using dataPersistor to get Form data, thought you're using getSession
$data = $this->getSession()->getFormData();
Save action
<?php
namespace Vendor\Module\Controller\Adminhtml\Scheduler;
use Magento\Framework\Exception\LocalizedException;
class Save extends \Magento\Backend\App\Action
{
protected $dataPersistor;
/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\App\Request\DataPersistorInterface $dataPersistor
) {
$this->dataPersistor = $dataPersistor;
parent::__construct($context);
}
/**
* Save actionSave action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$data = $this->getRequest()->getPostValue();
if ($data) {
$id = $this->getRequest()->getParam('scheduler_id');
$model = $this->_objectManager->create('Vendor\Module\Model\Scheduler')->load($id);
if (!$model->getId() && $id) {
$this->messageManager->addErrorMessage(__('This Scheduler no longer exists.'));
return $resultRedirect->setPath('*/*/');
}
$model->setData($data);
try {
$model->save();
$this->messageManager->addSuccessMessage(__('You saved the Scheduler.'));
$this->dataPersistor->clear('engraving_scheduler');
if ($this->getRequest()->getParam('back')) {
return $resultRedirect->setPath('*/*/edit', ['scheduler_id' => $model->getId()]);
}
return $resultRedirect->setPath('*/*/');
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the Scheduler.'));
}
$this->dataPersistor->set('engraving_scheduler', $data);
return $resultRedirect->setPath('*/*/edit', ['scheduler_id' => $this->getRequest()->getParam('scheduler_id')]);
}
return $resultRedirect->setPath('*/*/');
}
}
DataProvider
<?php
namespace Vendor\Module\Model\Scheduler;
use Vendor\Module\Model\ResourceModel\Scheduler\CollectionFactory;
use Magento\Framework\App\Request\DataPersistorInterface;
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
protected $dataPersistor;
protected $loadedData;
protected $collection;
/**
* Constructor
*
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param CollectionFactory $collectionFactory
* @param DataPersistorInterface $dataPersistor
* @param array $meta
* @param array $data
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
CollectionFactory $collectionFactory,
DataPersistorInterface $dataPersistor,
array $meta = [],
array $data = []
) {
$this->collection = $collectionFactory->create();
$this->dataPersistor = $dataPersistor;
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}
/**
* Get data
*
* @return array
*/
public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}
$items = $this->collection->getItems();
foreach ($items as $model) {
$this->loadedData[$model->getId()] = $model->getData();
}
$data = $this->dataPersistor->get('engraving_scheduler');
if (!empty($data)) {
$model = $this->collection->getNewEmptyItem();
$model->setData($data);
$this->loadedData[$model->getId()] = $model->getData();
$this->dataPersistor->clear('engraving_scheduler');
}
return $this->loadedData;
}
}
ui form xml
<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="provider" xsi:type="string">engraving_scheduler_form.scheduler_form_data_source</item>
<item name="deps" xsi:type="string">engraving_scheduler_form.scheduler_form_data_source</item>
</item>
<item name="label" translate="true" xsi:type="string">General Information</item>
<item name="config" xsi:type="array">
<item name="dataScope" xsi:type="string">data</item>
<item name="namespace" xsi:type="string">engraving_scheduler_form</item>
</item>
<item name="template" xsi:type="string">templates/form/collapsible</item>
<item name="buttons" xsi:type="array">
<item name="back" xsi:type="string">Vendor\Module\Block\Adminhtml\Scheduler\Edit\BackButton</item>
<item name="delete" xsi:type="string">Vendor\Module\Block\Adminhtml\Scheduler\Edit\DeleteButton</item>
<item name="save" xsi:type="string">Vendor\Module\Block\Adminhtml\Scheduler\Edit\SaveButton</item>
<item name="save_and_continue" xsi:type="string">Vendor\Module\Block\Adminhtml\Scheduler\Edit\SaveAndContinueButton</item>
</item>
</argument>
<dataSource name="scheduler_form_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">Vendor\Module\Model\Scheduler\DataProvider</argument>
<argument name="name" xsi:type="string">scheduler_form_data_source</argument>
<argument name="primaryFieldName" xsi:type="string">scheduler_id</argument>
<argument name="requestFieldName" xsi:type="string">scheduler_id</argument>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="submit_url" path="*/*/save" xsi:type="url"/>
</item>
</argument>
</argument>
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
</item>
</argument>
</dataSource>
<fieldset name="General">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string"/>
</item>
</argument>
<field name="label">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="label" translate="true" xsi:type="string">Title</item>
<item name="formElement" xsi:type="string">input</item>
<item name="source" xsi:type="string">Scheduler</item>
<item name="sortOrder" xsi:type="number">10</item>
<item name="dataScope" xsi:type="string">label</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
</item>
</item>
</argument>
</field>
</fieldset>
</form>
You will have to create a plugin for that. I wanted to add product flavor to order summary. This is the way I created a plugin and achieved what I wanted.
Vendor = Sejal
Files you need to create:
- Registration.php :
app\code\Sejal\Flavor\registration.php
- di.xml :
app\code\Sejal\Flavor\etc\di.xml
- module.xml :
app\code\Sejal\Flavor\etc\module.xml
- ConfigProviderPlugin.php :
app\code\Sejal\Flavor\Plugin\ConfigProviderPlugin.php
- details.html : copy of
vendor\magento\module-checkout\view\frontend\web\template\summary\item\details.html
you can override this file in your theme like this
app\design\frontend\Vendor\themename\Magento_Checkout\web\template\summary\item\details.html
Code:
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Sejal_Flavor',
__DIR__
);
di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Model\DefaultConfigProvider">
<plugin name="AddAttPlug" type="Sejal\Flavor\Plugin\ConfigProviderPlugin" />
</type>
</config>
module.xml
<?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="Sejal_Flavor" setup_version="1.0.0">
</module>
</config>
ConfigProviderPlugin.php
<?php
namespace Sejal\Flavor\Plugin;
class ConfigProviderPlugin extends \Magento\Framework\Model\AbstractModel
{
public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, array $result)
{
$items = $result['totalsData']['items'];
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
for($i=0;$i<count($items);$i++){
$quoteId = $items[$i]['item_id'];
$quote = $objectManager->create('\Magento\Quote\Model\Quote\Item')->load($quoteId);
$productId = $quote->getProductId();
$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
$productFlavours = $product->getResource()->getAttribute('flavors')->getFrontend()->getValue($product);
if($productFlavours == 'No' || $productFlavours == 'NA'){
$productFlavours = '';
}
$items[$i]['flavor'] = $productFlavours;
}
$result['totalsData']['items'] = $items;
return $result;
}
}
details.html
Copy vendor\magento\module-checkout\view\frontend\web\template\summary\item\details.html
in theme and add
<div class="product-item-flavor" data-bind="text: $parent.flavor"></div>
below
<strong class="product-item-name" data-bind="text: $parent.name"></strong>
That's it!
Hope it helps!
Best Answer
The sidebar on /checkout/#payment cannot be modified via xml.
The only way in the use case presented above is to override the core sidebar.html template with another one located in the custom module.
So in order to add a custom block on the right sidebar on the checkout payment page follow the steps below:
1- Create a requirejs-config.js in your custom module.
For example, at MyModule/view/frontend/requirejs-config.js
In the requirejs-config.js file, put the following declaration:
};
This is telling Magento to replace the default core sidebar.html with the one available in the custom module.
2- Customise your sidebar.html
Copy the core Magento_Checkout/template/sidebar.html file and paste it in your custom module as specified in the requirejs-config.js, at Namespace_MyModule/template/sidebar.html
Source and more informations: https://community.magento.com/t5/Programming-Questions/Custom-block-under-Order-Summary-block/m-p/66983#M2163