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>
The system.xml is a configuration file which is used to create configuration fields in Magento 2 System Configuration.
You will need this if your module has some settings which the admin needs to set. You can go to Store -> Setting -> Configuration to check how it look like.
To Create system.xml
- Step 1: Create System.xml
- Step 2: Set default value
- Step 3: Flush Magento cache
- Step 4: Get value from configuration
app/code/Mageplaza/HelloWorld/etc/adminhtml/system.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="mageplaza" translate="label" sortOrder="10">
<label>Mageplaza</label>
</tab>
<section id="helloworld" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>Hello World</label>
<tab>mageplaza</tab>
<resource>Mageplaza_HelloWorld::hello_configuration</resource>
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>General Configuration</label>
<field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Module Enable</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="display_text" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Display Text</label>
<comment>This text will display on the frontend.</comment>
</field>
</group>
</section>
</system>
</config>
Checking this code, you will see how to create a Tab, Section, Group and Field. We will find more detail about each element:
The Tab element may have many sections and some main attributes and child:
Id attribute is the identify for this tab
sortOrder attribute will define the position of this tab.
Translate attribute let Magento know which title need to translate
Label element child is the text which will show as tab title.
The Section element will have id, sortOrder, translate attributes like the Tab element. Some other attributes (showInDefault, showInWebsite, showInStore) will decide this element will be show on each scope or not. You can change the scope here
The section may have many group and some other child elements:
Class: this value will be added as class for this element. You should you it if you want to make-up this element.
Label: the text title of this element
Tab: this’s a tab id. This tab element will let Magento know the tab which this section is belong to. This section will be placed under that tab
Resource: defined the ACL rule which the admin user must have in order to access this configuration.
Group: This element may have many field and some attributes which is same as Sections.
Fields: is the main path of this page. It will save the data which we want to setting. In this element, we focus on the type attribute. It will define how the element is when display. It can be: text, select, file… In this example we create 2 fields with type select and text. With each type we will define the child element for the field to make it work as we want.
For example, with the type select/multiselect you must define the child element resource_model.
Step 2: Set default value
Each field in system.xml after create will not have any value. When you call them, you will receive ‘null’ result. So for the module, we will need to set the default value for the field and you will call the value without go to config, set value and save it. This default value will be saved in config.xml which is located in etc folder. Let’s create it for this simple configuration:
app/code/Mageplaza/HelloWorld/etc/config.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<hello>
<general>
<enable>1</enable>
<display_text>Hello World</display_text>
</general>
</hello>
</default>
</config>
Note that, if you might get an Error 404 Page Not Found first time, just logout and login again to fix this
Step 3: Flush Magento Cache
https://www.mageplaza.com/kb/how-flush-enable-disable-cache.html
Step 4: Get value from configuration
First all of let’s save value and flush cache, then you can get saved value from database.
In the system.xml, we have added 2 fields: enable and display_text. So the path should be:
helloworld/general/enable
helloworld/general/display_text
$this->scopeConfig->getValue('helloworld/general/enable', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$this->scopeConfig->getValue('helloworld/general/display_text', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
For more information use this link
Best Answer
Not sure if this a solution to your problem, but it can be a hint in the right direction.
You can specify a custom element rendered in any one of your form tabs like this (the example is from the catalog module):
The last method sets the element renderer block. You can try to use that but I think you will get some errors because that is destined for EAV entities.
You can create a block that extends
Mage_Adminhtml_Block_Catalog_Form_Renderer_Fieldset_Element
and add a custom template to it similar tocatalog/form/renderer/fieldset/element.phtml
where you can add your checkboxes or anything else.