Magento 2 – How to Override Adminhtml PHTML

adminhtmlmagento2overridesphtml

Does anyone know how to override the below template?

vendor\magento\module-backend\view\adminhtml\templates\media\uploader.phtml.

Best Answer

[EDIT] Override block - recommended way.

Override Block:

Create a new module.

app/code/Vendor/Backend/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\Media\Uploader" type="Vendor\Backend\Block\Media\Uploader" />
</config>

app/code/Vendor/Backend/Block/Media/Uploader.php

<?php

namespace Vendor\Backend\Block\Media;

class Uploader extends \Magento\Backend\Block\Media\Uploader
{
    protected $_template = 'Vendor_Backend::media/uploader.phtml';
}

Create a new Admin theme:

In this case, may we need to create new Admin theme:

Remember to specify the custom Admin theme in di.xml.

For example:

enter image description here

a) Create an admin theme first:

app/design/adminhtml/Vendor/CustomTheme/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::THEME,
    'adminhtml/Vendor/CustomTheme',
    __DIR__
);

app/design/adminhtml/Vendor/CustomTheme/theme.xml

<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
    <title>My custom Admin theme</title> <!-- your theme's name -->
    <parent>Magento/backend</parent> <!-- the parent theme. Example: Magento/backend -->
</theme>

b) Apply theme admin:

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

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Theme" setup_version="1.0.0">
        <sequence>>
            <module name="Magento_Theme"/>
        </sequence>
    </module>
</config>

app/code/Vendor/Theme/registration.php

<?php

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

app/code/Vendor/Theme/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">

    <!-- Admin theme. Start -->
    <type name="Magento\Theme\Model\View\Design">
        <arguments>
            <argument name="themes" xsi:type="array">
                <item name="adminhtml" xsi:type="string">Vendor/CustomTheme</item>
            </argument>
        </arguments>
    </type>
    <!-- Admin theme. End -->
</config>
Related Topic