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(), ] ); Related SolutionsMagento 2.1 – Show Textbox on Dropdown Change in Admin Form For field dependency in admin form, try below code $selectField = $fieldset->addField('select_field', 'select', array( 'label' => 'Show/Hide Selection', 'name' => 'select_field', 'values' => array( array( 'value' => 1, 'label' => 'Yes', ), array( 'value' => 0, 'label' => 'No', ), ) )); $hideField = $fieldset->addField('field_to_hide', 'text', ['name' => 'field_name','value'=> 'your_value', 'label' => __('Your label Name')] )); //code to dependence field $dependence = $this->getLayout()->createBlock( 'Magento\Backend\Block\Widget\Form\Element\Dependence') ->addFieldMap($selectField->getHtmlId(), $selectField->getName()) ->addFieldMap($hideField->getHtmlId(), $hideField->getName()) ->addFieldDependence( $hideField->getName(), $selectField->getName(), '1' ); $this->setChild('form_after', $dependence); Magento 2 – How to Add Dropdown in Admin Form Change Category class by following code: namespace Book\Flip\Model\Source; class Category implements \Magento\Framework\Option\ArrayInterface { /** * Retrieve options array. * * @return array */ public function toOptionArray() { $result = []; foreach (self::getOptionArray() as $index => $value) { $result[] = ['value' => $index, 'label' => $value]; } return $result; } /** * Retrieve option array * * @return string[] */ public static function getOptionArray() { return [1 => __('science')]; } /** * Retrieve option array with empty value * * @return string[] */ public function getAllOptions() { $result = []; foreach (self::getOptionArray() as $index => $value) { $result[] = ['value' => $index, 'label' => $value]; } return $result; } /** * Retrieve option text by option value * * @param string $optionId * @return string */ public function getOptionText($optionId) { $options = self::getOptionArray(); return isset($options[$optionId]) ? $options[$optionId] : null; } } Related TopicMagento 2 Admin – Dropdown Option Not Showing in Admin FormMagento 2 Admin Form – How to Add Static Options to DropdownMagento2 Admin Form – Create City State Country Dropdown UIComponent
Best Answer
You can use below code for add website drop down to admin form