TransWikia.com

Magento2: Conditionally replace block template and class the right way

Magento Asked by Riccardo Baral on November 23, 2021

This rather trivial requirement turns out be quite a pain for us in recent days, as we couldn’t find a satisfying existing solution, yet.

The Task

We are trying to replace certain blocks with a custom template accompanied with a custom block class. Our extension has a system configuration with a couple of boolean inputs (Yes/No), if certain predefined custom blocks should be used. Each custom block replaces one or more default blocks.

If the Merchant selects “No”, nothing should happen to these default blocks.

Example

The search box in the container header-wrapper in the Luma theme has a block named top.search. We wand to replace this block with the my-search.phtml template and the MySearch block class, which inherits from the default search block class. In this case MagentoFrameworkViewElementTemplate.


Current solutions

A) Using deprecated <action>-Tag with ifconfig-Attribute

My most favorable way was using layout xml files with basic layout definitions to fix our problem. We altered the layout as follows:

<referenceBlock name="top.search" class="TestMyAppBlockMySearch">
    <action ifconfig="myapp/components/searchbox" method="setTemplate">
        <argument name="template" xsi:type="string">Test_MyApp::my-search.phtml</argument>
    </action>
</referenceBlock>

Disadvantage: Magento2 documentation discourages from using <action>-Tag. If the search box feature is not used, it still overwrites the default block class.

B) Event-based block exchange using ObserverInterface

Disadvantage: It seems a bit hacky to me to use the event system in Magento2 to replace blocks on a certain condition. I get the feeling that this might also impact the performance, if a lot of blocks need to be replaced.

C) Insert a new and invisible block on every page that replaces default blocks on demand

// pseudocode
$layout = $this->getLayout();
$block = $layout->getBlock('top.search');
if ($block) {
  // todo only unset on condition
  $layout->unsetElement('top.search');

  $newBlock = $layout->createBlock('TestMyAppBlockMySearch', 'top.search', []);
  $layout->setBlock('top.search', $newBlock);
}

Disadvantage: Possible (unverified) performance and caching issues in the future. Similar to solution B.


The last solution (C) resembles our current pick. However, we are looking for a satisfying solution that works without using any PHP and is written in the Magento2 layout definition (XML). Any ideas?

One Answer

You can create a plugin for getTemplate() method for the desired block.

Answered by Roman Snitko on November 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP