3

My config file after save config data

enter image description here

After save data It also show error in console as there is no any field

after save data it does not give me unserialaize data my php file to render data in table is

<?php
        namespace Vendor\Module\Block\Adminhtml;

        use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
        use Magento\Framework\DataObject;
        use Magento\Framework\Exception\LocalizedException;
        use Vendor\Module\Block\Adminhtml\PaymentMethod;
        use Vendor\Module\Block\Adminhtml\UnitMethod;

        class Payment extends AbstractFieldArray
        {

            private $pymentRenderer;

            private $unitRenderer;


            protected function _prepareToRender()
            {
                $this->addColumn('paymentmethod', [
                    'label' => __('Payment Method'),
                    'renderer' => $this->getPaymentRenderer()
                ]);
                $this->addColumn('duration', ['label' => __('Duration'), 'class' => 'required-entry']);
                $this->addColumn('unit', [
                    'label' => __('Units'),
                    'renderer' => $this->getUnitRenderer()
                ]);
                $this->_addAfter = false;
                $this->_addButtonLabel = __('Add');
            }


            protected function _prepareArrayRow(DataObject $row): void
            {
                $options = [];

                $pm = $row->getPaymentMethod();
                $unit = $row->getUnitMethod();
                if ($pm !== null) {
                    $options['paymentmethod_' . $this->getPaymentRenderer()->calcOptionHash($pm)] = 'selected="selected"';
                    \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info(print_r($options,true));
                    $row->setData('paymentmethod', $options);
                }
                $options = [];
                if ($unit !== null) {
                    $options['unit_' . $this->getUnitRenderer()->calcOptionHash($unit)] = 'selected="selected"';
                    \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info(print_r($options,true));
                    $row->setData('unit', $options);
                }

            }


            private function getPaymentRenderer()
            {
                if (!$this->pymentRenderer) {
                    $this->pymentRenderer = $this->getLayout()->createBlock(
                        paymentmethod::class,
                        '',
                        ['data' => ['is_render_to_js_template' => true]]
                    );
                }
                return $this->pymentRenderer;
            }
            private function getUnitRenderer()
            {
                if (!$this->unitRenderer) {
                    $this->unitRenderer = $this->getLayout()->createBlock(
                        unitmethod::class,
                        '',
                        ['data' => ['is_render_to_js_template' => true]]
                    );
                }
                return $this->unitRenderer;
            }
        }
2
  • Did you find solution? I am facing same problem. Commented Jul 4, 2020 at 18:20
  • any solution on this issue? @Arjun Commented May 12, 2023 at 14:06

1 Answer 1

0

With reference to you console output, you need to modify your _prepareArrayRow function like this:

protected function _prepareArrayRow(DataObject $row): void
{
    $options = [];

    $pm = $row->getPaymentMethod();
    $unit = $row->getUnitMethod();
    if ($pm !== null) {
        $options['paymentmethod_' . $this->getPaymentRenderer()->calcOptionHash($pm)] = 'selected="selected"';
    }
    if ($unit !== null) {
        $options['unit_' . $this->getUnitRenderer()->calcOptionHash($unit)] = 'selected="selected"';

    }
    $row->setData('option_extra_attrs', $options);
}

Note: If you skip the label for dynamic field in system.xml, it won't save.

This is working on Magento 2.3.5 enterprise edition

Your Answer

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

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