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');
vendor/magento/module-product-video/view/adminhtml/templates/helper/gallery.phtml
should 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_ProductVideo
module uses an observer to set the template to begin with. – ryanF Jan 16 '17 at 20:48