1

I want to know if someone has already added a select field in a generated admin fields

Vendor/Name/Block/Adminhtml/Menu/Field/AdditionalMenu.php

<?php

namespace Vendor\Menu\Block\Adminhtml\Menu\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

/**
 * Class AdditionalMenu
 */
class AdditionalMenu extends AbstractFieldArray
{
    protected $_itemRenderer;
    /**
     * {@inheritdoc}
     */
    protected function _prepareToRender()
    {
        $this->addColumn(
            'Title',
            ['label' => __('Title'),
             'size' => '200px',
             'class' => 'required-entry']
        );
        //Option test
        /*$this->addColumn('Type',
        array('label'   => __('Type'),
              'type'    =>'options',
              'options' => array('Option 1', 'Option 2')
        ));*/
        $this->addColumn(
            'Link',
            ['label' => __('Link'),
             'size' => '200px']
        );
        $this->addColumn(
            'Ordre',
            ['label' => __('Ordre'),
             'size' => '50px',
             'class' => '']
        );
        $this->_addAfter = false;
        $this->_addButtonLabel = __('Ajouter un Menu');
    }

}

enter image description here

4 Answers 4

2

I found the solution:

How to add a select options column :

<?php    
namespace Vendor\Name\Block\Adminhtml\Menu\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

/**
 * Class AdditionalEmail
 */
class AdditionalMenu extends AbstractFieldArray
{
    protected $_typeblockOptions;
    protected $_cmsblockOptions;
    /**
     * {@inheritdoc}
     */
    protected function _prepareToRender()
    {
        $this->addColumn(
            'attributname',
            [
                'label' => __('Attributname'),
                'size' => '200px',
                'renderer' => $this->_getGroupRenderer()]
        );

        //OR
        $this->addColumn(
            'title',
            [
                'label' => __('Titre'),
                'size' => '200px',
                'class' => 'required-entry']
        );

        $this->_addAfter = false;
        $this->_addButtonLabel = __('Ajouter un Menu');

protected function _getGroupRenderer()
{
    if (!$this->_cmsblockOptions) {
        $this->_cmsblockOptions = $this->getLayout()->createBlock(
            \Vendor\Name\Block\Adminhtml\Menu\Config\Backend\Options::class,
            '',
            ['data' => ['is_render_to_js_template' => true]]
        );
        $this->_cmsblockOptions->setClass('attributname_group_select');
    }
    return $this->_cmsblockOptions;
}

Vendor\Name\Block\Adminhtml\Menu\Config\Backend\Options.php

<?php

namespace Vendor\Name\Block\Adminhtml\Menu\Config\Backend;

class Options extends \Magento\Framework\View\Element\Html\Select
{

    public function __construct(
        \Magento\Framework\View\Element\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }


    /**
     * @param string $value
     * @return $this
     */
    public function setInputName($value)
    {
        return $this->setName($value);
    }

    /**
     * Render block HTML
     *
     * @return string
     */
    public function _toHtml()
    {
        $html = '<select>';
        $html .= '<option value="First">First value</option>';
        $html .= '<option value="Second">Second value</option>';
        $html .= '</select>';
        return $html;
    }
}
3
  • could you please tell me _cmsblockOptions about this function i am getting error that function not found
    – Arunendra
    Nov 9, 2018 at 10:08
  • @Arunendra I updated a file
    – PЯINCƎ
    Nov 9, 2018 at 22:46
  • 1
    how to add a dynamic id and name for the select dropdown. rite now the following code doesnt save the data. May 3, 2019 at 9:36
2

Adding to the above answer, u can also just overwrite the getOptions Method from the extended \Magento\Framework\View\Element\Html\Select and return the toOptionArray Method of an existing SourceModel. Incase u just want to have a select inside of an AbstractFieldArray that is based on a SourceModel.

For example the Yesno Sourcemodel:

<?php

namespace Vendor\Name\Block\Adminhtml\Form\Field;

use Magento\Config\Model\Config\Source\Yesno as YesnoSource;
use Magento\Framework\View\Element\Context;
use Magento\Framework\View\Element\Html\Select;

/**
 * Class YesNo
 * @package Vendor\Name\Block\Adminhtml\Form\Field
 */
class Yesno extends Select
{
    /**
     * @var YesnoSource
     */
    protected $yesnoSource;

    /**
     * YesNo constructor.
     * @param Context $context
     * @param YesnoSource $yesnoSource
     * @param array $data
     */
    public function __construct(
        Context $context,
        YesnoSource $yesnoSource,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->yesnoSource = $yesnoSource;
    }

    /**
     * @return array|void
     */
    public function getOptions()
    {
        return $this->yesnoSource->toOptionArray();
    }
}

Which is pretty useful.

1

Researching some magento 2 documentation I find this, I think it might help you guys https://devdocs.magento.com/guides/v2.3/ext-best-practices/tutorials/dynamic-row-system-config.html

0

Adding to the PЯINCƎ's answer: for adding dynamic id and name to the select element, change code from

    $html = '<select>';

to

    $html = '<select id="' . $this->getInputId() . '" name="' . $this->getName() . '">';

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.