How to create store switcher in UI form Magento 2

Magento allows merchants to create multiple websites, stores and store views in single installation. While creating custom CRUD operation we might need to show a store switcher in UI form to maintain different values for different stores. Today’s blog will demonstrate how we can store switcher in UI form.

First step will be to defined the store switcher in layout file. Let’s assume we have layout file named as brand_index_edit.xml and this file is located inside view/adminhtml/layout directory of module.

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © WebbyTroops Technologies. All rights reserved.
 * See LICENSE.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="editor"/>
    <body>
        <referenceContainer name="content">
            <uiComponent name="brand_form"/>
        </referenceContainer>
        <referenceContainer name="page.main.actions">
            <block class="Magento\Backend\Block\Store\Switcher" name="store_switcher">
                <action method="setUseConfirm">
                    <argument name="params" xsi:type="string">1</argument>
                </action>
            </block>
        </referenceContainer>
    </body>
</page>

When you open the form you will be able to see the store switcher at top left same as product page. For any question please reach out from comment box. Happy Coding !!

Leave A Comment

Go To Top