57
votes
Magento 2: template security: which method to use?
The escaping methods in AbstractBlock all delegate calls to Magento\Framework\Escaper, so you'll find an overview there.
Let's look at the public methods and their documentation:
escapeHtml()
/**
*...
39
votes
How do I get the current product?
Although other answers are correct, they're not the recommended/proper solution either.
Using the ObjectManager is absolutely prohibited in Magento 2. So please don't rely on this solution, but use ...
31
votes
Accepted
When it is good to use viewModel ? What is difference between viewModel and Block?
Magento veteran Jessie has a well famous blog on viewModel which you can find it here. It answers your question at the end. It says:
When to use ViewModels?
When would you want to use ...
28
votes
Magento 2: How to add additional block on checkout page?
Here I give example to show custom block above shipping method of checkout
Create di.xml at
app/code/Vendor/Module/etc/frontend/di.xml
<?xml version="1.0"?>
<config xmlns:xsi=&...
25
votes
Accepted
Change page title in controler or block
You can do it in different ways - it's enough to use only one:
1. From controller
/** @var \Magento\Framework\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$...
23
votes
Magento 2 : load order by id in customer account order view
Try following way:
protected $orderRepository;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Sales\Api\OrderRepositoryInterface $...
20
votes
How do I get the current product?
If you are using Magento 2.1 or major, you can use this helper because the old method was deprecated.
...
use Magento\Catalog\Helper\Data;
...
public function __construct(
Context $context,
...
19
votes
Magento 2: Changing a Block's Template
The following worked for me in Magento EE 2.2.3
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/...
18
votes
How to override Virtual Type class in Magento 2
You have to use:
<preference for="Magento\CatalogSearch\Block\SearchResult\ListProduct" type="Vendor\Module\Block\Product\ListProductSearch" />
as Raphael mentioned.
To make this singular ...
18
votes
Accepted
Magento 2: template security: which method to use?
This is for Magento 2.0. For 2.1, refer to Fabian's answer
escapeHtml
Use this function in the case of a string output that should not contain HTML.
Example:
<span class='label'><?php ...
17
votes
Accepted
Add WYSIWYG Editor to Block Form in Magento 2
Discovered I was missing the Config setting:
/**
* @var \Magento\Cms\Model\Wysiwyg\Config
*/
protected $_wysiwygConfig;
/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \...
17
votes
Magento 2.2.0 - Display Recently Viewed Products block on Product Detail page
Unfortunately the accepted answer didn't work for me and I wasn't able to use widget instance layout updates in my use case
If for whatever reason you need to add a 'Recently Viewed' products block ...
16
votes
Magento 2 Store switcher error in "section-config.js" Uncaught TypeError: Cannot read property '*' of undefined(…)
I had the same problem and found out that these two blocks where not called any more due to customizations in the theme.
The two blocks are per default added in the <referenceContainer name="...
15
votes
Accepted
How to use escapeHtml in Magento 2?
/**
* Escaper
*
* @var \Magento\Framework\Escaper
*/
protected $_escaper;
public function __construct(
\Magento\Framework\Escaper $_escaper
) {
$this->_escaper=$_escaper
}
Now you can ...
15
votes
Accepted
Remove apply discount code from Cart
app/design/frontend/{Vendor}/{theme}/Magento_Checkout/layout/checkout_cart_index.xml
<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
...
13
votes
Accepted
Magento 2 how to get sum of a column?
$collection = $this->customFactory->create()->addFieldToFilter('status',2) ->addFieldToFilter('customer_id',2);
$collection->getSelect()->columns(['total' => new \Zend_Db_Expr('...
12
votes
Magento 2 How to Remove product information tabs
When looking in /slippers.nl/vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml you can see how it is added to the page.
<block class="Magento\Catalog\Block\...
12
votes
How to use escapeHtml in Magento 2?
With the latest version of Magento 2.4 now you can directly use $escaper variable to use class \Magento\Framework\Escaper. The $escaper local variable is available inside the any .phtml templates.
<...
11
votes
Accepted
Magento2: How to remove the newsletter block?
You can remove this block completely via layout with remove="true":
app/design/frontend/TemplateMonster/Spiceli/Magento_Newsletter/layout/default.xml
<?xml version="1.0"?>
<page xmlns:xsi="...
11
votes
Magento 2 : load order by id in customer account order view
Declare \Magento\Sales\Api\OrderRepositoryInterface in construct of your class.
Try following snippet:
protected $orderRepository;
public function __construct(
\Magento\Sales\Api\...
11
votes
Accepted
Magento2 : Call getViewFileUrl() function in block file
Use \Magento\Framework\View\Asset\Repository class to get view file url in block file
protected $_assetRepo;
public function __construct(
...
\Magento\Framework\View\Asset\Repository $...
11
votes
Magento 2 How to get only one field data in collection
Magento has an inbuilt function on Magento\Framework\Data\Collection which provide a specific field value from the collection.
You have to use getColumnValues($colName) for getting the specific field ...
10
votes
Accepted
magento 2 - How to create a custom "admin" block and display it on an existing Admin page?
Traditional location for admin blocks is (Example):
app/code/YourVendor/YourModule/Block/Adminhtml/...
If you'd like to use block with phtml template you'd like to extend
\Magento\Framework\View\...
10
votes
addAttributeToSelect Call to undefined method M2
You have to replace addAttributeToSelect('*') with addFieldToSelect('*')
addAttributeToFilter() is used to filter EAV collections.
addFieldToFilter() is used to filter Non-EAV collections.
10
votes
Accepted
How to call phtml file from custom theme to cms block
File location will be
app/design/frontend/CustomTheme/CustomChild/Magento_Theme/templates/html/test.phtml
calling a block
{{block class="Magento\Framework\View\Element\Template" name="testblock" ...
10
votes
Accepted
Magento 2 - Call list phtml by passing collection variable
First you need to create block Vendor\Module\Block\ProductList.php by overriding this function _getProductCollection() by extending your class from this Magento\Catalog\Block\Product\ListProduct.
...
10
votes
Accepted
Magento 2 - Call different module block function in my custom phtml
You Can Call Another Module Block Like This :-
$block = $this->getLayout()->createBlock('\Vendor\Module\Block\FileName')->yourFunction();
9
votes
Accepted
Difference Between $block and $this magento 2
Regardless of what object in template you use ($block or $this) always will be called method from $block.
Magic happens in \Magento\Framework\View\TemplateEngine\Php::__call().
public function ...
9
votes
Accepted
Magento 2: Add block above page-header
Change your default.xml to
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/...
9
votes
Invalid template file override Magento 2 block
In the file customer_address_index.xml you can see this:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
blocks × 1815magento2 × 681
layout × 458
magento-1.9 × 318
template × 213
module × 128
overrides × 108
magento-1 × 98
xml × 92
magento-2.1 × 83
phtml × 83
controllers × 73
cms × 66
product × 64
adminhtml × 61
magento-1.7 × 58
magento-1.8 × 51
cache × 47
static-block × 47
php × 46
category × 41
custom-block × 40
frontend × 37
error × 30
event-observer × 29