cancel
Showing results for 
Search instead for 
Did you mean: 

Use of str_replace in .phtml File

Use of str_replace in .phtml File

In My Magento 2 Home Page, If I insert Widgets in content, then my product image is not showing.at frontend

Path Navigation from Admin: Content -> Pages -> select the Home Page -> Edit -> Content -> Insert Widget of Widget Type Catalog New Products list

 

After inserting Widget, my content is updated with the below code:

{{widget type="Magento\Catalog\Block\Product\Widget\NewWidget" display_type="all_products" show_pager="0" products_count="10" template="product/widget/new/content/new_grid.phtml"}}

 

But if I call those product images by creating anther Widget of Widget Type Catalog New Products list and specifying them in CMS Home Page in Design Layout, my product images are displaying properly.

 

I found the reason behind this that when I am inserting Widget in page content , my image path is not being set properly

 

My Correct Image Path URL under root directory

/media/catalog/product/cache/158ebf59ec3448c88300639f82de1259//b/e/benefits-coffee-14.jpg

 

My Wrong Image Path URL under root director

/media/catalog/product%5Ccache%5C158ebf59ec3448c88300639f82de1259%5C//b/e/benefits-coffee-14.jpg

Issue is %5C should have to be transformed into \ i.e. URL encoding is not happening when I am inserting Widget in page content 

So, I want to use str_replace to replace %5C to \ forcefully but yet not successful.

I am using Magento Community Edition, Magento ver. 2.4.3-p1

 

Please suggest me how to resolve such issue 

Thanks,

Anik

3 REPLIES 3

Re: Use of str_replace in .phtml File

Inside a custom module, it is a little bit harder to change templates. Here is an example of changing topmenu.html coming from the module Magento_Theme. In your module the template needs to be placed here:

app/code/${VENDORNAME}/${MODULENAME}/view/frontend/templates/html/topmenu.phtml


Additionally, a layout definition is required:

app/code/${VENDORNAME}/${MODULENAME}/view/frontend/layout/default.xml

 

<?xml version="1.0"?>
<page layout="1columns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock class="Magento\Theme\Block\Html\Topmenu" name="catalog.topnav" template="${VENDORNAME}_${MODULENAME}::html/topmenu.phtml" ttl="false"/>
</body>
</page>

Have a look at app/code/Magento/Theme/view/frontend/layout/default.xml for reference, where this XML crap can be found

If issue solved, Click Kudos & Accept as Solution.
LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool

Re: Use of str_replace in .phtml File

Hi Expert,

I am using Magento default blank theme.

The relative path for default.xml from root directory is 

\vendor\magento\module-theme\view\frontend\layout\default.xml

Under default.xml my area of concern is

<container name="content" label="Main Content Area"/> => this is line no 112

I want to replace widget image path so that my images can be visible. 

I am using module-catalog.

 

Please suggest.

Thanks,

Anik

Re: Use of str_replace in .phtml File

hi anik, were you able to solve this issue?