4

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?

Khoa Truong
32.5k11 gold badges91 silver badges159 bronze badges
asked Jan 15, 2017 at 13:39
6
  • The link you posted shows you the most direct and simplest way to "extend" (really "override") an existing template with your own. I'd suggest checking to see that you've correctly created the appropriate folder hierarchy in your custom module to match what the devdocs state. Furthermore, you might want to check the basis...is your module enabled? Have you cleared all the generated files (or cleared cache) before loading that page (to verify your template is getting loaded instead of the core template)? Commented Jan 15, 2017 at 15:07
  • This is my path: magento\htdocs\app\design\adminhtml\Amasty\HelloWorld\ProductVideo\templates\helper\gallery.phtml Should this work? Commented Jan 15, 2017 at 15:10
  • @Roninio you create a custom module or custom admin theme? Commented Jan 15, 2017 at 15:29
  • Sorry for the confusion. I want to set a custom module, as i want to be able to make logical changes. Commented Jan 15, 2017 at 15:31
  • @Roninio There's some confusion here. After revisiting that link you posted, that method is specifically for using a custom theme, not a custom module. If you were to use a custom theme to override the vendor/magento/module-product-video/view/adminhtml/templates/helper/gallery.phtml should be magento\htdocs\app\design\adminhtml\Amasty\HelloWorld\Magento_ProductVideo\view\adminhtml\templates\hel‌​per\gallery.phtml . Otherwise, you'd use something like what @Khoa TruongDinh suggested. Furthermore, the Magento_ProductVideo module uses an observer to set the template to begin with. Commented Jan 16, 2017 at 20:48

1 Answer 1

0

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.php
  • vendor/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');
answered Jan 15, 2017 at 14:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.