Magento – Magento 2: How to add websites dropdown in admin form

magento-2.1magento2

Magento 2: Code to add "website" dropdown in admin form

Best Answer

You can use below code for add website drop down to admin form

    /**
     * @var \Magento\Store\Model\System\Store
     */
    protected $_systemStore;


 public function __construct(
        \Magento\Store\Model\System\Store $systemStore,
    ) {
        $this->_systemStore = $systemStore;
     }

In function _prepareForm() add column

$fieldset->addField(
                'website_id',
                'select',
                [
                    'name' => 'website_id',
                    'label' => __('Associate to Website'),
                    'title' => __('Associate to Website'),
                    'required' => true,
                    'values' => $this->_systemStore->getWebsiteValuesForForm(),

            ]
        );