0

I want to add Installed version of current extension/module in admin menu like below screenshot. https://i.imgur.com/0P7QZSA.png

Note: For creating menus in m2 admin, I am using following code from /etc/adminhtml/menu.xml file.

 <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
        <menu>
            <add
                id="My_Module::main"
                title="My Module"
                module="My_Module"
                sortOrder="999"
                parent="Magento_Backend::stores"
                resource="Magento_Backend::stores"
            />
            <add
                id="My_Module::storeCredentials"
                title="Store Credentials"
                module="My_Module"
                sortOrder="10"
                parent="My_Module::main"
                action="adminhtml/system_config/edit/section/My_Module_credentials/store/1/"
                resource="My_Module::main"
            />
            <add
        </menu>
    </config>

4 Answers 4

0

I found this link but in here they are adding menu programmatically, I suggest you add your menu using menu.xml only.

And for answer of your question, add any custom text manually in menu.xml just like you did in the screenshot, because when will you change the version of your module you have to change it in your module.xml and composer.json file, in this file you have to change your module version manually so you can do same in the menu.xml

2
  • thank you for the answer. But I am looking for something that can help me in fetching the Installed version in xml file. As I have already a function in my Block and looking for some way to use it in xml file. Commented Dec 27, 2023 at 10:10
  • check my second answer, and if works then please accept it and upvote it, thanks:) Commented Dec 27, 2023 at 11:15
0

You need to add it manually on your menu.xml we can't add that version dynamically in menu.xml

title="My Module"

replace with

title="My Module (v2.0.1)"

1
  • thank you for the answer. But adding the version statically will not help me. Commented Dec 27, 2023 at 10:06
0
  1. Update menu.xml:

    File Path: app/code/Vendor/Module/etc/adminhtml/menu.xml

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
        <menu>
            <add id="Vendor_Module::main_menu" title="My Module" module="Vendor_Module" sortOrder="10" resource="Vendor_Module::main_menu">
                <item name="transaction" xsi:type="array">
                    <item name="name" xsi:type="string" translate="true">Transaction</item>
                    <item name="id" xsi:type="string">Vendor_Module::transaction</item>
                    <item name="title" xsi:type="string" translate="true">Transaction</item>
                    <item name="module" xsi:type="string">Vendor_Module</item>
                    <item name="parent" xsi:type="string">Vendor_Module::main_menu</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                    <item name="action" xsi:type="string">vendor_module/transaction/index</item>
                </item>
                <item name="history" xsi:type="array">
                    <item name="name" xsi:type="string" translate="true">History</item>
                    <item name="id" xsi:type="string">Vendor_Module::history</item>
                    <item name="title" xsi:type="string" translate="true">History</item>
                    <item name="module" xsi:type="string">Vendor_Module</item>
                    <item name="parent" xsi:type="string">Vendor_Module::main_menu</item>
                    <item name="sortOrder" xsi:type="number">20</item>
                    <item name="action" xsi:type="string">vendor_module/history/index</item>
                </item>
            </add>
        </menu>
    </config>
    

After updating the menu.xml file, make sure to run bin/magento setup:upgrade to apply the changes. Now, your "My Module" menu should have "Transaction" and "History" as submenus in Magento 2.4.

0

Not with menu.xml but you can add your text with this,

app/code/Custom/Module/etc/adminhtml/di.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="Magento\Backend\Block\AnchorRenderer" type="Custom\Module\Block\AnchorRenderer"/>
    </config>

app/code/Custom/Module/Block/AnchorRenderer.php

    <?php

    namespace Custom\Module\Block;

    use Magento\Backend\Model\Menu\Item;
    use Magento\Backend\Block\MenuItemChecker;
    use Magento\Framework\Escaper;

    class AnchorRenderer extends \Magento\Backend\Block\AnchorRenderer
    {
        /**
         * @var MenuItemChecker
         */
        private $menuItemChecker;

        /**
         * @var Escaper
         */
        private $escaper;

        /**
         * @param MenuItemChecker $menuItemChecker
         * @param Escaper $escaper
         */
        public function __construct(
            MenuItemChecker $menuItemChecker,
            Escaper $escaper
        ) {
            $this->menuItemChecker = $menuItemChecker;
            $this->escaper = $escaper;
        }

        /**
         * Render menu item anchor.
         *
         *  It is used in backend menu to render anchor menu.
         *
         * @param Item|false $activeItem Can be false if menu item is inaccessible
         * but was triggered directly using controller. It is a legacy code behaviour.
         * @param Item $menuItem
         * @param int $level
         * @return string
         */
        public function renderAnchor($activeItem, Item $menuItem, $level)
        {
            if ($level == 1 && $menuItem->getUrl() == '#') {
                $output = '';
                if ($menuItem->hasChildren()) {
                    if ($menuItem->getTitle() == 'your_custom_menu_title') {
                        $output = '<strong class="submenu-group-title" role="presentation">'
                            . '<span>' . $this->escaper->escapeHtml(__($menuItem->getTitle() . 'Your Custom Text')) . '</span>'
                            . '</strong>';
                } else {
                    $output = '<strong class="submenu-group-title" role="presentation">'
                        . '<span>' . $this->escaper->escapeHtml(__($menuItem->getTitle())) . '</span>'
                        . '</strong>';
                }
            }
        } else {
            $target = $menuItem->getTarget() ? ('target=' . $menuItem->getTarget()) : '';
            $output = '<a href="' . $menuItem->getUrl() . '" ' . $target . ' ' . $this->_renderItemAnchorTitle(
                $menuItem
            ) . $this->_renderItemOnclickFunction(
                $menuItem
            ) . ' class="' . ($this->menuItemChecker->isItemActive($activeItem, $menuItem, $level) ? '_active' : '')
                . '">' . '<span>' . $this->escaper->escapeHtml(__($menuItem->getTitle()))
                . '</span>' . '</a>';
        }

        return $output;
    }

    /**
     * Render menu item anchor title
     *
     * @param Item $menuItem
     * @return string
     */
    private function _renderItemAnchorTitle($menuItem)
    {
        return $menuItem->hasTooltip() ? 'title="' . __($menuItem->getTooltip()) . '"' : '';
    }

    /**
     * Render menu item onclick function
     *
     * @param Item $menuItem
     * @return string
     */
    private function _renderItemOnclickFunction($menuItem)
    {
        return $menuItem->hasClickCallback() ? ' onclick="' . $menuItem->getClickCallback() . '"' : '';
    }
}

It will show you like this in admin

enter image description here

Hope this will work for you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.