I want to create an extension that will give shop owners ability to edit their product image. So I understand I need this template
Magento/ProductVideo/view/adminhtml/templates/helper/gallery.phtml
I have tried the following guide http://devdocs.magento.com/guides/v2.1/frontend-dev-guide/templates/template-walkthrough.html with no success.
How can I extend this template in my extension? What are my options?
1 Answer 1
As far as I know, there are some ways:
1) We can override the Block. In your custom block, we need to set the template variable: (not recommend)
/**
* @var string
*/
protected $_template = 'Vendor_Module::catalog/product/helper/gallery.phtml'; // Your custom template.
2) In case, we can set the template via layout (recommended way)
For example, we need to set the custom Add to Card template.
Vendor/Module/view/frontend/layout/checkout_cart_configure.xml
<referenceBlock name="product.info.addtocart">
<action method="setTemplate">
<argument name="template" xsi:type="string">Vendor_Module::cart/item/configure/updatecart.phtml</argument>
</action>
</referenceBlock>
In your case, I search throught the Magento 2 source, I saw two places set the template helper/gallery.phtml.
vendor/magento/module-catalog/Block/Adminhtml/Product/Helper/Form/Gallery/Content.phpvendor/magento/module-product-video/Observer/ChangeTemplateObserver.php
a) If you want to set the template, you should use 2nd way.
b) In case using Observer, we can override the Observer(not recommend). Or, disable this Observer and use our custom Observer.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_product_gallery_prepare_layout">
<observer name="change_template" disabled="true"/>
<observer name="change_template_custom" instance="Vendor\Module\Observer\ChangeTemplateObserver" disabled="false"/>
</event>
</config>
Our ChangeTemplateObserver observer:
$observer->getBlock()->setTemplate('Vendor_Module::helper/gallery.phtml');
Explore related questions
See similar questions with these tags.
vendor/magento/module-product-video/view/adminhtml/templates/helper/gallery.phtmlshould bemagento\htdocs\app\design\adminhtml\Amasty\HelloWorld\Magento_ProductVideo\view\adminhtml\templates\helper\gallery.phtml. Otherwise, you'd use something like what @Khoa TruongDinh suggested. Furthermore, theMagento_ProductVideomodule uses an observer to set the template to begin with.