1

I am trying to create a custom module with a block that should get the values of variables in view.xml. How can I do this?

3 Answers 3

0

For better understanding please have a look this view

https://github.com/magento/magento2/blob/2.4.0/app/code/Magento/Catalog/etc/view.xml

with the following implementation

https://github.com/magento/magento2/blob/2.4.0/app/code/Magento/Catalog/Model/Product/Image/ParamsBuilder.php#L173

0

You can get the variables from etc/view.xml files in a couple of ways.

Let's assume we want to get the value of product_image_size from the Magento_Wishlist module... https://github.com/magento/magento2/blob/2.4.4/app/code/Magento/Wishlist/etc/view.xml

In a Template (.phtml) File

<?= $block->getVar('product_image_size', 'Magento_Wishlist') ?> 

Remember to properly escape this output depending on how you are using it (not shown)

In a PHP Class

$this->viewConfig->getViewConfig() ->getVarValue('Magento_Wishlist', 'product_image_size'); 

Here is an example within a full custom block...

<?php namespace YourNamespace\YourModule\Block; use Magento\Framework\View\Element\Template; use Magento\Framework\View\ConfigInterface; use Magento\Framework\View\Element\Template\Context; class YourBlock extends Template { /** * @var ConfigInterface */ protected $viewConfig; /** * Constructor * * @param Context $context * @param ConfigInterface $viewConfig * @param array $data */ public function __construct( Template\Context $context, ConfigInterface $viewConfig, array $data = [] ) { $this->viewConfig = $viewConfig; parent::__construct($context, $data); } /** * Get product_image_size * * @return int */ public function getProductImageSize(): int { return (int) $this->viewConfig->getViewConfig() ->getVarValue('Magento_Wishlist', 'product_image_size'); } } 
-1

I found the answer here. It provides all the needed steps you do to get the variable values in view.xml https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/themes/theme-images.html#view_xml_vars

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.