Adding a Store View Selector to Magento 2 Admin Toolbar
As an experienced Magento developer, I often need to create modules that support different configurations for various store views within the same installation. Having a store view selector in the admin toolbar would greatly simplify managing these settings. In this post, we will walk through adding a store view selector to your custom admin module’s toolbar using a combination of built-in Magento 2 features and some custom code.
Understanding the Current Toolbar Setup
First, let’s review the current state of your toolbar setup based on the provided code snippet:
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();
}
}
This code adds a single “Save” button to the toolbar using Magento 2’s built-in getToolbar() method. The toolbar is an instance of \Magento\Backend\Block\Widget\Container, which provides a simple way to add child blocks (in this case, the save button).
Extending the Toolbar with Store View Selector
To extend the toolbar and include a store view selector similar to the one used for editing products, you’ll need to modify your _prepareLayout() method. The key is to use Magento 2’s addChild() method in conjunction with the appropriate block type.
First, let’s define the new block class that will handle the store view selection:
class StoreViewSelector extends \Magento\Backend\Block\Widget\Form\Container
{
protected function _construct()
{
$this->_objectId = 'store_view';
$this->_controller = 'adminhtml_storeview';
$this->_blockGroup = 'Magento_Store';
parent::_construct();
}
}
This StoreViewSelector class extends the built-in \Magento\Backend\Block\Widget\Form\Container, which is responsible for handling form containers in the admin area. We define some basic properties to match the structure of Magento’s default store view management.
Next, update your existing Edit class to include the new selector:
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();",
]
);
$this->getToolbar()->addChild(
'store_view_selector',
'\Magento\Store\Block\Adminhtml\Widget\Form',
[
'id' => 'store_views',
'template' => 'widget/form/grid.phtml',
'form_id' => 'mp_mymodule_edit_form',
'action' => '*/*/save',
'data_attribute' => [
'target' => '#mp_mymodule_edit_form',
],
]
);
return parent::_prepareLayout();
}
}
In this updated version, we add a new child block called store_view_selector using the \Magento\Store\Block\Adminhtml\Widget\Form class. This is the same block type used for the store view selector in the product editing section of the admin.
We configure some key properties:
'id' => 'store_views'sets the unique ID for this block.'template' => 'widget/form/grid.phtml'specifies the template file responsible for rendering the store view selection as a grid layout.'form_id' => 'mp_mymodule_edit_form'links this selector to your module’s form ID, ensuring changes are submitted properly.'action' => '*/*/save'sets the action URL where the selected store view data will be processed (replace*/*/savewith your actual controller and action).'data_attribute' => ['target' => '#mp_mymodule_edit_form']ensures that the form submission targets your module’s edit form.
Conclusion
Adding a store view selector to your custom Magento 2 admin module toolbar is possible by leveraging the built-in getToolbar() method and extending it with appropriate child blocks. By using the \Magento\Store\Block\Adminhtml\Widget\Form block along with the correct template and configuration, you can create a functional and integrated store view selector for your custom admin form.
Remember to replace placeholders like 'mp_mymodule_edit_form' with your actual form ID and update the action URL accordingly. Also, ensure that your module’s controller handles the selected store view data appropriately when processing form submissions.
Don’t forget to clear your cache after making these changes and verify that the new selector appears in your admin toolbar alongside your existing save button.
For more detailed information on Magento 2 block and layout management, feel free to explore our comprehensive guides at Laravel Company.