0

I've made a new module. Here, I've override the AbstractFieldArray class.

    <?php
namespace myVendor\myModule\Block\System\Form\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use myVendor\myModule\Block\System\Form\Field\ShowReasonColumn;

class Multi extends AbstractFieldArray {
    /**
     * @var \Magento\Framework\Data\Form\Element\Factory
     */
    protected $_elementFactory;
    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Data\Form\Element\Factory $elementFactory
     * @param array $data
     */

    private $showReason;

    protected $_template = 'myVendor_myModule::system/config/form/field/array.phtml';


    public function __construct(
    \Magento\Backend\Block\Template\Context $context, \Magento\Framework\Data\Form\Element\Factory $elementFactory, array $data = []
    ) {
        $this->_elementFactory = $elementFactory;
        parent::__construct($context, $data);
    }

    protected function _prepareToRender() {
        $this->addColumn('id', ['label' => __('ID'), 'readonly'=>'readonly']);
        $this->addColumn('message', ['label' => __('Message'), 'class' => 'required-entry']);

        $this->addColumn('show_reason_frontend', [
            'label' => __('Showin Frontend'),
            'renderer' => $this->getShowFrontend()
        ]);

        $this->_addAfter = false;
        $this->_addButtonLabel = __('Add');
 
    }


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

        $showFrontend = $row->getShowFrontend();


        if ($showFrontend !== null) {
            $options['option_' . $row->getShowFrontend()] = 'selected="selected"';
        }
        
        $row->setData('option_extra_attrs', $options);
    }

    private function getShowFrontend()
    {
        if (!$this->showReason) {
            $this->showReason = $this->getLayout()->createBlock(
                ShowReasonColumn::class,
                '',
                ['data' => ['is_render_to_js_template' => true]]
            );
        }
        return $this->showReason;
    }
}

My output in admin is this:

enter image description here

I would like an auto-incremented ID (it should show the row number, Ex: 1,2)

P.S. I don't have a delete action so it needs only to increment every time I'm adding a new row. Also, I will use this ID in my DB.

1
  • Use this for auto increment: $this->addColumn('id', [ 'auto_increment' => true, 'nullable' => false, 'primary' => true ,'label' => __('ID'), 'readonly'=>'readonly']); May 8, 2021 at 12:06

1 Answer 1

0

If you use this field for the ID in your DB then you don't need to display it on the admin page. Because when saving to DB it will automatically generate ID in ascending order. As far as your photo is in the ID field your client can edit it, when you use this for the ID in your DB it will cause a lot of errors because the guest can enter any number you can't control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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