1

I created dropdown in a table similar to Store > Config > Catalog > Invenotry, everything is working perfectly except selected is not working in my dropdown, however values are stored in a database.

I search in core files and follows:

Magento\CatalogInventory\Block\Adminhtml\Form\Field\Minsaleqty

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


protected function _prepareToRender()
{
 $this->addColumn(
     'dayoption',
     [
         '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, '',
     ['data' => ['is_render_to_js_template' => true]]
 );
 $this->_itemRenderer->setClass('option_group_select');
 return $this->_itemRenderer;
}

 /**
  * Prepare existing row data object
  *
  * @param \Magento\Framework\DataObject $row
  * @return void
  */
 protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
 {
     $optionExtraAttr = [];
     $optionExtraAttr['option_' . $this->_getRenderer()->calcOptionHash($row->getData('dayoption'))] =
         'selected="selected"';
     $row->setData(
         'option_extra_attrs',
         $optionExtraAttr
     );
 }

File: Option.php

use Magento\Framework\View\Element\Html\Select;

class Options extends Select
{
/**
 * @var \Magento\Eav\Model\Config
 */
protected $_eavConfig;
/**
 *
 * @var array
 */
private $_attributeGroups;

public function __construct(Context $context,
                            \Magento\Eav\Model\Config $eavConfig,
                            array $data = [])
{
    parent::__construct($context, $data);
    $this->_eavConfig = $eavConfig;
}

public function setInputName($value)
{
    return $this->setName($value);
}

public function _attributeOptions(){
    if ($this->_attributeGroups === null) {
        $this->_attributeGroups = [];

        $attributeCode = "delivery_days";
        $attribute = $this->_eavConfig->getAttribute('catalog_product', $attributeCode);
        $options = $attribute->getSource()->getAllOptions();
        $attrValues = [];
        foreach ($options as $option) {
            if ($option['value'] > 0) {
                $this->_attributeGroups[$option['label']] = $option['label'];
            }
        }
    }
    return $this->_attributeGroups;
}

/**
 * Render block HTML
 *
 * @return string
 */
  public function _toHtml()
  {
    if (!$this->getOptions()) {
        foreach ($this->_attributeOptions() as $item) {
            $this->addOption($item, addslashes($item));
        }
    }
    return parent::_toHtml();
  }      
}

_prepareArrayRow is reason for selected in value in a dropdown, I print value of optionExtraAttr which is

array ( 'option_614544684' => 'selected="selected"',)

currently after loading the page its shows first value of dropdown, not which is store in core_config_data.

Thanks

1 Answer 1

0

I follow dev-docs & finds few mistakes in my Template.php

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

class Template extends AbstractFieldArray
{
 /**
  * @var Options
  */
 protected $_optionRenderer;
 /**
 * Prepare rendering the new field by adding all the needed columns
 */
protected function _prepareToRender()
{
    $this->addColumn(
        'dayoption',
        [
            'label' => __('Delivery Days'),
            'renderer' => $this->_getRenderer(),
            'class' => 'required-entry'
        ]
    );
    $this->_addAfter = false;
    $this->_addButtonLabel = __('Add More');
}

/**
 * Prepare existing row data object
 *
 * @param \Magento\Framework\DataObject $row
 * @return void
 */
protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
{
    $options = [];
    $attribute = $row->getDayoption();
    if ($attribute !== null) {
        $options['option_' . $this->_getRenderer()->calcOptionHash($attribute)] = 'selected="selected"';
    }

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

  /**
   * @return Options
   * @throws LocalizedException
   */
  private function _getRenderer()
  {
    if (!$this->_optionRenderer) {
        $this->_optionRenderer = $this->getLayout()->createBlock(
            Options::class,
            '',
            ['data' => ['is_render_to_js_template' => true]]
        );
    }
    return $this->_optionRenderer;
  }
}

The reason is I am not getting value for selected in _prepareArrayRow function which now I am getting from $attribute = $row->getDayoption();

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.