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:
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.