I am following the answer to creating a table with dropdown in Store>Configuration.
Its looks good but not storing my selected dropdown value in core_config_data
table.
currently its storing null array, if I remove dropdown and add text field than its working completely fine and also store data in database, so I think I made some mistake for creating dropdown.
Here is my code:
File: system.xml
<field id="template" translate="label" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Select Template</label>
<frontend_model>Vendor\Module\Block\Adminhtml\Form\Field\Template</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
</field>
File: Template.php
class Template extends AbstractFieldArray
{
protected $_itemRenderer;
protected function _prepareToRender()
{
$this->addColumn(
'deliverydays',
[
'label' => __('Delivery Days'),
'readonly'=>'readonly',
'renderer' => $this->_getRenderer()
]
);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add More');
}
protected function _getRenderer()
{
$this->_itemRenderer = $this->getLayout()->createBlock(
\Vendor\Module\Block\Adminhtml\Form\Field\Options::class, '',
array('is_render_to_js_template' => true)
);
return $this->_itemRenderer;
}
File: Options.php
class Options extends Select
{
/**
* @var \Magento\Eav\Model\Config
*/
protected $_eavConfig;
public function __construct(Context $context,
\Magento\Eav\Model\Config $eavConfig,
array $data = [])
{
parent::__construct($context, $data);
$this->_eavConfig = $eavConfig;
}
public function _toHtml()
{
$html = '<select>';
foreach ($this->attributeOptions() as $column):
$html .= '<option value="'.$column.'">'.$column.'</option>';
endforeach;
$html .= '</select>';
return $html;
}
public function setInputName($value)
{
return $this->setName($value);
}
public function attributeOptions(){
$attributeCode = "delivery_days";
$attribute = $this->_eavConfig->getAttribute('catalog_product', $attributeCode);
$options = $attribute->getSource()->getAllOptions();
$attrValues = [];
foreach ($options as $option) {
if ($option['value'] > 0) {
$attrValues[] = $option['label']; //->getText(); //use '->getText' if attribute is system generated
}
}
return $attrValues;
}
}