I try to load the current product in gallery.phtml
Helper (app\code\Company\Base\Helper\Data.php):
...
use \Magento\Catalog\Helper\Data as CatalogHelper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
...
/**
* @var \Magento\Catalog\Model\Product
*/
protected $_product;
protected $_catalogHelper;
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\View\Layout\ProcessorFactory $layoutProcessorFactory,
\Magento\Framework\View\Asset\Repository $assetRepo,
\Magento\Cms\Model\BlockFactory $blockFactory,
\Magento\Cms\Api\BlockRepositoryInterface $blockRepository,
\Magento\Cms\Block\Block $cmsblock,
\Magento\Catalog\Model\CategoryRepository $categoryRepository,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Customer\Model\Session $customerSession,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Directory\Model\CurrencyFactory $currencyFactory,
\Magento\Catalog\Model\Product $product,
\Magento\Framework\App\Http\Context $httpContext,
\Magento\Framework\ObjectManagerInterface $interface,
\Magento\Cms\Model\Template\FilterProvider $filterProvider,
\Magento\Framework\Filesystem\DirectoryList $dir,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
\Magento\Customer\Model\Customer $customer,
\Magento\Customer\Model\Address $address,
\Magento\Framework\App\Request\Http $request,
CatalogHelper $catalogHelper,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
$this->_layoutProcessorFactory = $layoutProcessorFactory;
$this->_assetRepo = $assetRepo;
$this->blockFactory = $blockFactory;
$this->_blockRepository = $blockRepository;
$this->cmsblock = $cmsblock;
$this->categoryRepository = $categoryRepository;
$this->storeManager = $storeManager;
$this->_customerSession = $customerSession;
$this->_checkoutSession = $checkoutSession;
$this->_product = $product;
$this->_httpContext = $httpContext;
$this->_object = $interface;
$this->_currencyCode = $currencyFactory->create();
$this->_scopeConfig = $scopeConfig;
$this->_customer = $customer;
$this->_address = $address;
$this->_filterProvider = $filterProvider;
$this->_dir = $dir;
$this->_request = $request;
$this->_customerRepositoryInterface = $customerRepositoryInterface;
$this->_store = $storeManager->getStore();
$this->_catalogHelper = $catalogHelper;
}
/**
* @return Product
*/
public function getProduct()
{
if (is_null($this->_product)) {
$this->_product = $this->_catalogHelper->getProduct();
}
// DEBUG
var_dump($this->_product->getData()); exit(); // Output: array(0) { }
return $this->_product;
}
public function getProductName()
{
return $this->getProduct()->getName();
}
...
}
Layout (THEME\Magento_Catalog\layout\override\base\catalog_product_view.xml):
<container name="product.info.media" htmlTag="div" htmlClass="product media" after="product.info.main">
<block class="Magento\Catalog\Block\Product\View\Gallery"
name="product.info.media.image"
template="Magento_Catalog::product/view/gallery.phtml">
<arguments>
<argument name="gallery_options" xsi:type="object">Magento\Catalog\Block\Product\View\GalleryOptions</argument>
<argument name="imageHelper" xsi:type="object">Magento\Catalog\Helper\Image</argument>
<argument name="product_name" xsi:type="helper" helper="Company\Base\Helper\Data::getProductName" />
</arguments>
</block>
</container>
gallery.phtml (THEME\Magento_Catalog\templates\product\view\gallery.phtml):
$productName = $block->getProductName();
exit("'".$productName."'");
Output:
array(0) { }