I create custom module for add rule condition in custom module but not get condition data on edit time here i share my files
vendor_module_form.xml
<fieldset name="conditions_fieldset">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Conditions</item>
<item name="collapsible" xsi:type="boolean">false</item>
</item>
</argument>
<container name="conditions_serialized_container" >
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="sortOrder" xsi:type="number">10</item>
</item>
</argument>
<htmlContent name="html_content">
<argument name="block" xsi:type="object">VendorModuleBlockAdminhtmlMessageEditTabConditions</argument>
</htmlContent>
</container>
</fieldset>
VendorModuleBlockAdminhtmlMessageEditTabConditions.php
<?php
namespace VendorModuleBlockAdminhtmlMessageEditTab;
use MagentoBackendBlockWidgetTabTabInterface;
use MagentoBackendBlockWidgetFormGeneric;
class Conditions extends Generic implements TabInterface
{
protected $rendererFieldset;
protected $conditions;
public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoFrameworkRegistry $registry,
MagentoFrameworkDataFormFactory $formFactory,
MagentoRuleBlockConditions $conditions,
MagentoBackendBlockWidgetFormRendererFieldset $rendererFieldset,
VendorModuleModelRuleFactory $labelFactory,
array $data = []
) {
$this->rendererFieldset = $rendererFieldset;
$this->conditions = $conditions;
$this->labelFactory = $labelFactory;
parent::__construct($context, $registry, $formFactory, $data);
}
public function getTabLabel()
{
return __("Conditions");
}
public function getTabTitle()
{
return __("Conditions");
}
public function canShowTab()
{
return true;
}
public function isHidden()
{
return false;
}
protected function _prepareForm()
{
$model = $this->_coreRegistry->registry("current_label");
$form = $this->addTabToForm($model);
$this->setForm($form);
return parent::_prepareForm();
}
protected function addTabToForm(
$model,
$fieldsetId = "conditions_fieldset",
$formName = "message_form"
) {
if (!$model) {
$id = $this->getRequest()->getParam("id");
$model = $this->labelFactory->create();
$model->load($id);
}
$newChildUrl = $this->getUrl(
"msg/message/newConditionHtml/form/conditions_fieldset"
);
/** @var MagentoFrameworkDataForm $form */
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix("rule_");
$renderer = $this->rendererFieldset
->setTemplate("Magento_CatalogRule::promo/fieldset.phtml")
->setNewChildUrl($newChildUrl);
$fieldset = $form
->addFieldset($fieldsetId, [
"legend" => __(
"Apply the rule only if the following conditions are met (leave blank for all products)."
),
])
->setRenderer($renderer);
$fieldset
->addField("conditions", "text", [
"name" => "conditions",
"label" => __("Conditions"),
"title" => __("Conditions"),
"required" => true,
"data-form-part" => $formName,
])
->setRule($model)
->setRenderer($this->conditions);
$form->setValues($model->getData());
$this->setConditionFormName($model->getConditions(), $formName);
return $form;
}
private function setConditionFormName(
MagentoRuleModelConditionAbstractCondition $conditions,
$formName
) {
$conditions->setFormName($formName);
if (
$conditions->getConditions() &&
is_array($conditions->getConditions())
) {
foreach ($conditions->getConditions() as $condition) {
$this->setConditionFormName($condition, $formName);
}
}
}
}
VendorModuleControllerAdminhtmlMessageNewConditionHtml.php
<?php
namespace VendorModuleControllerAdminhtmlMessage;
class NewConditionHtml extends VendorModuleControllerAdminhtmlMessageRule
{
/**
* New condition html action
*
* @return void
*/
public function execute()
{
$id = $this->getRequest()->getParam('id');
$labelFactory = $this->_objectManager->create('VendorModuleModelRuleFactory');
$typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
$type = $typeArr[0];
$model = $this->_objectManager->create(
$type
)->setId(
$id
)->setType(
$type
)->setRule(
$labelFactory->create()
)->setPrefix(
'conditions'
);
if (!empty($typeArr[1])) {
$model->setAttribute($typeArr[1]);
}
if ($model instanceof VendorModuleModelConditionAbstractCondition) {
$model->setFormName('message_form')->setJsFormObject('conditions_fieldset');
$html = $model->asHtmlRecursive();
} else {
$html = '';
}
$this->getResponse()->setBody($html);
}
}
VendorModuleControllerAdminhtmlMessageSave.php
<?php
namespace VendorModuleControllerAdminhtmlMessage;
use MagentoBackendAppAction;
use MagentoBackendModelSession;
use VendorModuleModelMessageFactory;
class Save extends MagentoBackendAppAction
{
/*
* @var Message
*/
protected $messageModel;
/**
* @var Session
*/
protected $adminsession;
/**
* @param ActionContext $context
* @param Message $messageModel
* @param Session $adminsession
*/
public function __construct(
ActionContext $context,
MessageFactory $messageModel,
Session $adminsession
) {
parent::__construct($context);
$this->messageModel = $messageModel;
$this->adminsession = $adminsession;
}
/**
* Save message record action
*
* @return MagentoBackendModelViewResultRedirect
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$model = $this->messageModel->create();
$data = $this->getRequest()->getPostValue();
$customerGroup = implode(',',$data['customer_group']);
$data['customer_group'] = $customerGroup;
$website = implode(',',$data['website']);
$data['website'] = $website;
// echo "<pre>";
// print_r($data);
if ($data) {
$id = $this->getRequest()->getParam('id');
if ($id) {
$data['updated_at'] = date("Y-m-d H:i:s");
$model->load($id);
}
if (isset($data['rule']['conditions'])) {
$data['conditions'] = json_encode($data['rule']['conditions']);
}
unset($data['rule']);
// print_r($data['conditions']);
// exit;
$model->setData($data);
try {
$model->save();
$this->messageManager->addSuccess(__('The data has been saved.'));
$this->adminsession->setFormData(false);
if ($this->getRequest()->getParam('back')) {
if ($this->getRequest()->getParam('back') == 'add') {
return $resultRedirect->setPath('*/*/add');
} else {
return $resultRedirect->setPath('*/*/edit', ['id' => $model->getId(), '_current' => true]);
}
}
return $resultRedirect->setPath('*/*/');
} catch (MagentoFrameworkExceptionLocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (RuntimeException $e) {
$this->messageManager->addError($e->getMessage());
} catch (Exception $e) {
$this->messageManager->addException($e, __('Something went wrong while saving the data.'));
}
$this->_getSession()->setFormData($data);
return $resultRedirect->setPath('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
}
return $resultRedirect->setPath('*/*/');
}
}
VendorModuleModelDataProvider.php
<?php
namespace VendorModuleModel;
use VendorModuleModelResourceModelMessageCollectionFactory;
class DataProvider extends MagentoUiDataProviderAbstractDataProvider
{
private $loadedData;
/**
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param CollectionFactory $messageCollectionFactory
* @param array $meta
* @param array $data
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
CollectionFactory $messageCollectionFactory,
array $meta = [],
array $data = []
) {
$this->collection = $messageCollectionFactory->create();
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 $blog) {
$this->loadedData[$blog->getId()] = $blog->getData();
$this->loadedData['conditions_fieldset'] = $blog->getData('conditions_fieldset');
}
return $this->loadedData;
}
}