2

I want to override this file in my custom module:

/var/www/magento2-dev/vendor/magento/module-theme/view/adminhtml/templates/title.phtml

How can I achieve this?

Thanks for any help!

2

Yes you can override it. Create a simple module like below:

Step 1: Create registration.php like below.

app/code/Vendor/Module/registration.php

with below content:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

Step 2: Create module.xml like below.

app/code/Vendor/Module/etc/module.xml

with below content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="0.0.1"></module>
        <sequence>
            <module name="Magento_Backend"/>
             <module name="Magento_Sales"/>
            <module name="Magento_Quote"/>
            <module name="Magento_Checkout"/>
        </sequence>
</config>

Step 3: Create default.xml like below.

app/code/Vendor/Module/view/adminhtml/layout/default.xml

with below content:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <body>
        <referenceBlock name="page.title">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Vendor_Module::title.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

Step 4: Create title.phtml like below.

app/code/Vendor/Module/view/adminhtml/templates/title.phtml

with below content:

// @codingStandardsIgnoreFile

/**
 * @var $block \Magento\Theme\Block\Html\Title
 */
$titleId = ($block->getTitleId()) ? ' id="' . $block->getTitleId() . '"' : '';
$titleClass = ($block->getTitleClass()) ? ' ' . $block->getTitleClass() : '';
$title = $block->escapeHtml($block->getPageTitle());
?>

<div class="page-title-wrapper<?= /* @escapeNotVerified */ $titleClass ?>">
    <h1 class="page-title"<?= /* @escapeNotVerified */ $titleId ?>><?= /* @escapeNotVerified */ $title ?></h1>
    <?= $block->getChildHtml() ?>
</div>

Add something to H1 tag to test if its working or not.

Flush the magento cache and test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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