7

I could not find how to extend backend template files in Magento 2, however i have found how to extend frontend template files from here. If I extend the file at design/adminhtml/Magento/backend it may override in Magento upgrades version. So I want to write on design/adminhtml/vendor/backend and module specific folders. However i tried this by placing the required phtml files, but it is not showing from my path, its taking from default magento path.

C:\xampp\htdocs\NewMagento2\app\design\adminhtml\vendor\backend\Magento_Customer\layout\customer_form.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="form">
            <block class="Magento\Customer\Block\Adminhtml\Edit\Tab\View" name="customer_edit_tab_view" template="tab/view.phtml">
                <arguments>
                    <argument name="tab_label" xsi:type="string">Customer View</argument>
                    <argument name="sort_order" xsi:type="number">10</argument>
                </arguments>
                <block class="Magento\Customer\Block\Adminhtml\Edit\Tab\View\PersonalInfo" name="personal_info" template="tab/view/personal_info.phtml"/>
            </block>
        </referenceBlock>
    </body>
</page>

C:\xampp\htdocs\NewMagento2\app\design\adminhtml\Vendor\backend\Magento_Customer\templates\tab\view.phtml

<?php
echo $this->getChildHtml();

And if i use templatehints from backend still its showing from magento default i.e. C:/xampp/htdocs/NewMagento2/app/code//Magento/Customer/view/adminhtml/templates/tab/view.phtml

Help me in detail on how to extend Magento2 admin template files

1
  • I found @Marius related answer here. But at last not concluded the answer in the comments by Marius Commented Aug 4, 2015 at 12:20

3 Answers 3

9

I'm given example here of how to extend the sales order view page. Like this, you can extend any module which one you want. Follow the Below Steps

Step 1) Enable the extension to specify the config.php file from the app/etc folder

<?php
return array (
  'modules' => 
  array (
        'Learning_RewriteSales' => 1,
  ),
);

Step 2) Create Custom.php Class in Block

<?php
namespace Learning\RewriteSales\Block\Adminhtml\Order\View;

class Custom extends \Magento\Backend\Block\Template
{

}

Step 3) Create Info.php file to extend core Info.php

<?php
namespace Learning\RewriteSales\Block\Adminhtml\Order\View;

class Info extends \Magento\Sales\Block\Adminhtml\Order\View\Info
{

}

Step 4) Create di.xml file to specify which class you were extended ( Dependency Injection )

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Block\Adminhtml\Order\View\Info" type="Learning\RewriteSales\Block\Adminhtml\Order\View\Info"/>
</config>

Step 5) Create module.xml file to specify the setup version

<?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="Learning_RewriteSales" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>

Step 6) Create sales_order_view.xml file from Learning/RewriteSales/view/layout folder and write the below code

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="order_info">
            <action method="setTemplate">
                <argument name="template" translate="true" xsi:type="string">order/view/info.phtml</argument>
            </action>
        </referenceBlock>
        <referenceBlock name="order_info">
            <block class="Learning\RewriteSales\Block\Adminhtml\Order\View\Custom" name="sales_order_view_custom" template="order/view/custom.phtml" />
        </referenceBlock>
    </body>
</page>

Step 7) Create the Custom.phtml and Info.phtml file from Learning/RewriteSales/view/layout and insert below code.

Custom.phtml

<h1>Hi, I am here!</h1>

Info.phtml

First Copy the code from Magento\Sales\view\adminhtml\templates\order\view\info.phtml file and past here after that add one more line

<?php echo $block->getChildHtml('sales_order_view_custom');?>

Step 8) Finally clean the cache and see your custom code Sales Order View Page.

It's working for me. If you have any queries let me know?

2
  • This is very helpful, thanks! What if I want to do something even simpler than this: what if I just want to override a style in magento2\vendor\magento\theme-adminhtml-backend\web\css\source\forms\_controls.less? I hope you can answer my question that I posted separately.
    – thdoan
    Commented Jun 16, 2016 at 8:03
  • ranasohel.me/2015/10/02/…
    – Jackson
    Commented Nov 29, 2016 at 9:42
0

And this is my .js file code.

defaults: {
                template: 'Magento_SamplePaymentGateway/payment/form',
                medicaidId: '',
                dateOfBirth: '',
            },

            initObservable: function () {
                this._super()
                    .observe('medicaidId'); 
                this._super()
                    .observe('dateOfBirth');
                return this;
            },

            getCode: function() {
                return 'sample_gateway';
            },

            getData: function() {

                return {
                    'method': this.item.method,
                    "additional_data":{
                        'medicaid_id': this.medicaidId(),
                        'do_birth': this.dateOfBirth()                                              
                        }
                };

            },
0

And this is my Observer

protected $additionalInformationList = [
        'medicaid_id', 'do_birth'        
    ];
public function execute(Observer $observer)
    {
        $data = $this->readDataArgument($observer);
        $additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA);
        //$this->_logger->log(100,print_r($additionalData,true));
        if (!is_array($additionalData)) {

            return;
        }
        $paymentInfo = $this->readPaymentModelArgument($observer);
        //$this->_logger->log(100,print_r($paymentInfo,true));

        foreach ($this->additionalInformationList as $additionalInformationKey) {
            if (isset($additionalData[$additionalInformationKey])) {
                $paymentInfo->setAdditionalInformation(
                    $additionalInformationKey,
                    $additionalData[$additionalInformationKey]
                );
            }
        }
}

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.