Question

I am creating a custom module and using Magneto's core framework to render a select box using code as below:

$this->getLayout()->createBlock(
            'Magento\Framework\View\Element\Html\Select'
        )->setName(
            'mycustomname'
        )->setId(
            'mycustomid'
        )->setTitle(
            'my custom title'
        )->setValue(
            $selectedValue
        )->setOptions(
            $myoptionsarray
        )->getHtml();

I get the select box fine but I want to pass extra parameters to select box and I tried following options just before getHtml() but none of them worked for me:

->setExtraParams('disabled = disabled')
->setExtraParams('disabled = true')

can anyone help me regarding this if I want to pass extra parameters to the element

---------- UPDATE --------

if I check core file at: Magento\Framework\View\Element\Html\Select

 protected function _toHtml()
    {
        if (!$this->_beforeToHtml()) {
            return '';
        }

        $html = '<select name="' .
            $this->getName() .
            '" id="' .
            $this->getId() .
            '" class="' .
            $this->getClass() .
            '" title="' .
            $this->escapeHtml($this->getTitle()) .
            '" ' .
            $this->getExtraParams() .
            '>';

I can see this, which says to getExtraParams() at the end but dont see that working

This one finally worked for me after I ran setup:upgrade

->setExtraParams('disabled=true')

No correct solution

OTHER TIPS

Try this

$this->getLayout()->createBlock(
            'Magento\Framework\View\Element\Html\Select'
        )->setName(
            'mycustomname'
        )->setId(
            'mycustomid'
        )->setTitle(
            'my custom title'
        )->setValue(
            $selectedValue
        )->setOptions(
            $myoptionsarray
        )->setAttribute('disabled', 'disabled')->getHtml();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top