3

I am trying to implement Magento 2 catalog rule condtions in a custom module Ui Component form. Below is the field set:

<htmlContent name="block_promo_catalog_edit_tab_conditions" sortOrder="20">
    <argument name="data" xsi:type="array">
        <item name="wrapper" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Conditions</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="opened" xsi:type="boolean">false</item>
        </item>
    </argument>
    <settings>
        <wrapper>
            <canShow>true</canShow>
            <componentType>fieldset</componentType>
        </wrapper>
    </settings>
    <block class="Magento\CatalogRule\Block\Adminhtml\Promo\Catalog\Edit\Tab\Conditions" name="promo_catalog_edit_tab"/>
</htmlContent>

I am getting below error:

Fatal error: Method Magento\Ui\TemplateEngine\Xhtml\Result::__toString() must not throw an exception, caught Error: Call to a member function getConditionsFieldSetId() on null in /vendor/magento/module-ui/Component/Wrapper/UiComponent.php on line 0

Any idea how to fix this one.?

If we implement the same field for Sales Rule it works fine with the following code without error:

<fieldset name="conditions">
<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">true</item>
        <item name="sortOrder" xsi:type="number">127</item>
    </item>
</argument>
<container name="conditions_apply_to" >
    <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">Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Conditions</argument>
    </htmlContent>

</container>

1 Answer 1

0

Add this file in Block folder

Vendor\Module\Block\Adminhtml\Index\Edit\Tab

namespace Vendor\Module\Block\Adminhtml\Index\Edit\Tab;

use Magento\Framework\App\ObjectManager;
use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Backend\Block\Widget\Tab\TabInterface;

class Conditions extends Generic implements TabInterface
{
/**
 * Core registry
 *
 * @var \Magento\Backend\Block\Widget\Form\Renderer\Fieldset
 */
protected $_rendererFieldset;

/**
 * @var \Magento\Rule\Block\Conditions
 */
protected $_conditions;

private $ruleFactory;

/**
 * Initialize dependencies.
 *
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Framework\Registry $registry
 * @param \Magento\Framework\Data\FormFactory $formFactory
 * @param \Magento\Rule\Block\Conditions $conditions
 * @param \Magento\Backend\Block\Widget\Form\Renderer\Fieldset $rendererFieldset
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\Data\FormFactory $formFactory,
    \Magento\Rule\Block\Conditions $conditions,
    \Magento\Backend\Block\Widget\Form\Renderer\Fieldset $rendererFieldset,
    array $data = []
) {
    $this->_rendererFieldset = $rendererFieldset;
    $this->_conditions = $conditions;
    parent::__construct($context, $registry, $formFactory, $data);
}


private function getRuleFactory()
{
    if ($this->ruleFactory === null) {
        $this->ruleFactory = ObjectManager::getInstance()->get('Magento\CatalogRule\Model\RuleFactory');
    }
    return $this->ruleFactory;
}

/**
 * {@inheritdoc}
 * @codeCoverageIgnore
 */
public function getTabClass()
{
    return null;
}



/**
 * {@inheritdoc}
 * @codeCoverageIgnore
 */
public function getTabLabel()
{
    return __('Conditions');
}

/**
 * {@inheritdoc}
 * @codeCoverageIgnore
 */
public function getTabTitle()
{
    return __('Conditions');
}

/**
 * {@inheritdoc}
 * @codeCoverageIgnore
 */
public function canShowTab()
{
    return true;
}

/**
 * {@inheritdoc}
 * @codeCoverageIgnore
 */
public function isHidden()
{
    return false;
}

protected function _prepareForm()
{
    $model = $this->_coreRegistry->registry('\Magento\CatalogRule\Controller\RegistryConstants::CURRENT_CATALOG_RULE_ID');

    if (!$model) {
        $id = $this->getRequest()->getParam('rule_id');
        $model = $this->getRuleFactory()->create();
        $model->load($id);
    }
    $fieldsetId = 'conditions_fieldset';
    $formName = 'catalog_rule_form';

    /** @var \Magento\Framework\Data\Form $form */
    $form = $this->_formFactory->create();
    $form->setHtmlIdPrefix('rule_');

    $newChildUrl = $this->getUrl(
        'catalog_rule/promo_catalog/newConditionHtml/form/' . $model->getConditionsFieldSetId($formName),
        ['form_namespace' => $formName]
    );

    $renderer = $this->_rendererFieldset->setTemplate('Magento_CatalogRule::promo/fieldset.phtml')
                        ->setNewChildUrl($newChildUrl)
                        ->setFieldSetId($model->getConditionsFieldSetId($formName));

    $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')]
    )->setRule(
        $model
    )->setRenderer(
        $this->_conditions
    );

    $form->setValues($model->getData());
    $this->setConditionFormName($model->getConditions(), $formName);
    $this->setForm($form);

    return parent::_prepareForm();
}

}

And Add this code in layout edit file

<referenceContainer name="left">
    <block class="Vender\Module\Block\Adminhtml\Chartlist\Edit\Tabs" name="vender_module_edit_tabs">
        <block class="Vender\Module\Block\Adminhtml\Chartlist\Edit\Tab\Conditions" name="vender_module_tab_conditions"/>
        <action method="addTab">
            <argument name="name" xsi:type="string">conditions_section</argument>
            <argument name="block" xsi:type="string">vender_module_tab_conditions</argument>
        </action>
    </block>
</referenceContainer>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.