Adding Custom Tabs to CMS Pages via a Custom Module in Magento
As a senior developer at LaravelCompany, I often receive queries about extending the functionality of Magento’s built-in features using custom modules. One common request is adding extra tabs for CMS pages, which isn’t straightforward out-of-the-box. Let me guide you through this process step-by-step.
Understanding the Magento CMS Page Structure
Magento’s CMS page configuration is managed by the cms_page table in your database and rendered via the adminhtml_cms_page_edit.xml layout file. To add a new tab, we need to modify this structure using our custom module.
Creating Your Custom Module
First, ensure you have created a new module following Magento’s naming conventions (e.g., Magento_MyModule). Place your module in app/code/Magento/MyModule.
In your module, create the necessary files:
etc/adminhtml/cms_page_edit.xmlfor layout modificationsetc/adminhtml/menu.xmlto add a menu itemBlock/CustomTab.phpfor your custom tab content
Modifying the Layout
Open app/code/Magento/MyModule/etc/adminhtml/cms_page_edit.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocationgeries= "urn:magento:framework:Config/etc/config.xsd">
<menu>
<add id="Magento_MyModule::cms_page_custom_tab"
title="Custom Tab"
module="Magento_MyModule"
sortOrder="50"
action="mycustomtab/index/edit" />
</menu>
<layout xmlns:ui="urn:magento:framework:UrlInterface">
<adminhtml_cms_page_edit>
<referenceContainer name="content">
<block class="Magento\MyModule\Block\CustomTab" before="-" template="mycustomtab.phtml"/>
</referenceContainer>
</adminhtml_cms_page_edit>
</layout>
</config>
This XML file adds a new menu item and places our custom tab content block right before the existing “Content” tab.
Creating the Custom Tab Block
Create app/code/Magento/MyModule/Block/CustomTab.php:
<?php
namespace Magento\MyModule\Block;
class CustomTab extends \Magento\Backend\Block\Widget {
protected function _toHtml(){
return $this->getLayout()->createBlock('Magento\Cms\Block\Adminhtml\Page\Edit\Tab\Content')->setTemplate('mycustomtab.phtml');
}
}
This block simply renders the content template we’ll create next.
Creating the Content Template
Create app/code/Magento/MyModule/view/adminhtml/templates/mycustomtab.phtml:
<div class="admin__page-edit-content">
<h3>My Custom Tab Content</h3>
<!-- Add your custom tab HTML content here -->
</div>
Registering Your Routes
In app/code/Magento/MyModule/etc/adminhtml/routes.xml, ensure you have the following:
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App etc/routes.xsd">
<route id="mycustomtab" frontName="mycustomtab">
<area>adminhtml</area>
</route>
</routes>
This registers the new route for your custom tab.
Conclusion
Adding a custom tab to Magento’s CMS page involves modifying the layout, creating a block and template, and registering the necessary routes. By following this guide, you should now be able to add extra tabs to your CMS pages using a custom module.
Remember that Magento development requires a solid understanding of XML layouts, blocks, templates, and routing. If you need more complex modifications or have other questions about Magento development, I encourage you to visit LaravelCompany’s Magento Development Services page for expert assistance.