Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add store view selector to admin toolbar in Magento 2

I am creating a module which will support different configuration settings for different store views and it would be great to have a store view selector similar to the one which appears when one is editing a product in the admin.

I have managed to add buttons to my module toolbar using the code:

class Edit extends \Magento\Backend\Block\Template
{   
    protected function _prepareLayout()
    {
        $this->getToolbar()->addChild(
            'save_button',
            'Magento\Backend\Block\Widget\Button',
            [
                'label' => __('Save'),
                'data_attribute' => [
                    'role' => 'save',
                ],
                'class' => 'save primary',
                'onclick' => "jQuery('#mp_mymodule_edit_form').submit();",
            ]
        );
        return parent::_prepareLayout();
    }
}

I wondered if it was possible to insert the store view selector using Tools::addChild method? Looked around Stack Overflow and Google in general and have not managed to find any thing on this. Fingers crossed, someone does know.

Thanks in advance

like image 823
musaffar.patel Avatar asked Apr 09 '16 12:04

musaffar.patel


People also ask

How do I change the store view in Magento 2?

1. Go to Stores > All Stores and choose the Store you want to change the default store view for. 2. In the Default Store View section set the Store View you want to be a default one for this particular store and press Save Store button.


1 Answers

Eventually managed to work this out by poking around various Magento files, posting here in case anyone is looking for the same solution:

Method 1 - Adding to the _prapareLayout function:

    $this->getToolbar()->addChild(
        'store_switcher',
        'Magento\Backend\Block\Store\Switcher'
    );

Method 2 - Layout XML file (in my case I added this to my layout file in app/code/MP/MyModule/view/adminhtml/layout/productpricebysize_dimension_edit

<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>
like image 171
musaffar.patel Avatar answered Oct 19 '22 20:10

musaffar.patel