{"id":1246,"date":"2020-09-14T12:42:04","date_gmt":"2020-09-14T12:42:04","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/09\/14\/add-store-view-control-in-admin-form-in-magento-2\/"},"modified":"2025-05-22T11:47:40","modified_gmt":"2025-05-22T06:17:40","slug":"add-store-view-control-in-admin-form-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-store-view-control-in-admin-form-in-magento-2\/","title":{"rendered":"How to Add Store View Control in Admin Form in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 supports multiple store views and the admin can manage the features and functionalities based on store views. It can be even easier with the below method to add store view control in admin form in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if you have multiple store views based on the locations or languages. Now, a customer has added an FAQ or your developer has implemented a location-based feature. Such things need to be enabled only for a specific store view.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In such a scenario, you can add store view control in Magento 2 admin form as shown here:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/09\/Store-View-Controller-in-Admin-Form-in-Magento-2-1.png\" alt=\"Store View Controller in Admin Form in Magento 2\" class=\"wp-image-10478\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You can even enable a custom extension for a particular store view only with the below method:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Steps to Add Store View Control in Admin Form in Magento 2<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Block\\Adminhtml\\Questions\\Edit\\Tab;\n\nuse Magento\\Backend\\Block\\Store\\Switcher\\Form\\Renderer\\Fieldset\\Element;\nuse Magento\\Backend\\Block\\Template\\Context;\nuse Magento\\Backend\\Block\\Widget\\Form\\Generic;\nuse Magento\\Framework\\Data\\FormFactory;\nuse Magento\\Framework\\Registry;\nuse Magento\\Store\\Model\\System\\Store;\n\nclass Questions extends Generic\n{\n\n    protected $systemStore;\n\n    public function __construct(\n        Store $systemStore,\n        Context $context,\n        Registry $registry,\n        FormFactory $formFactory,\n        array $data = []\n    ) {\n        parent::__construct($context, $registry, $formFactory, $data);\n        $this->systemStore = $systemStore;\n    }\n    protected function _prepareForm()\n    {\n        $model = $this->_coreRegistry->registry('row_data');\n        $form = $this->_formFactory->create();\n        $fieldset = $form->addFieldset(\n            'field_set_id',\n            ['legend' => __('form title')]\n        );\n\n        $field = $fieldset->addField(\n            'store_id',\n            'select',\n            [\n                'label' => __('Store View'),\n                'title' => __('Store View'),\n                'name' => 'store_id',\n                'value' => $model->getStoreId(),\n                'values' => $this->systemStore->getStoreValuesForForm(false, true)\n\/\/                set first argument true and second to false to add blank option which value is blank\n\/\/                set second argument true to add \"All Store Views\" option which value is 0\n            ]\n        );\n        $renderer = $this->getLayout()->createBlock(\n            Element::class\n        );\n        $field->setRenderer($renderer);\n\n        $form->setValues($model->getData());\n        $this->setForm($form);\n\n        return parent::_prepareForm();\n    }\n\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it to control the features and functions of the Magento 2 store based on the store view.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You may also find this blog post helpful \u2013&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-get-all-store-ids-names-codes\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 API \u2013 Get All Store IDs, Names, and Codes<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do not forget to share this method with Magento 2 store admins via social media who are managing multiple store views.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 supports multiple store views and the admin can manage the features and functionalities based on store views. It can be even easier with&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1246","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1246"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1246\/revisions"}],"predecessor-version":[{"id":14676,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1246\/revisions\/14676"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}