Question

To clarify, I have two(2) modules. One provided by a third-party vendor and one I have created. The first piece of code is from the third-party vendor setting the "default" success_message.

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<container name="root">
    <block class="Magento\Catalog\Block\Product\View" as="thirdpartyvendor_success_message"
           template="Thirdpartyvendor_Module::thirdpartyvendor_success_message.phtml">
    </block>
</container>
</layout>

I have a custom product page being loaded where I need to override the thirdpartyvendor_success_message.phtml with custom_success_message.phtml inside my custom page layout. I have tried overriding the block(thirdpartyvendor_success_message) with a referenceBlock and setTemplate method as detailed below but this does not work.

<?xml version="1.0"?>
  <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

    ...

      <referenceBlock name="thirdpartyvendor_success_message">
        <action method="setTemplate">
        <argument name="template" xsi:type="string">Mycompany_Module::custom_success_message.phtml</argument></action>
      </referenceBlock>

    ...

  </body>
</page>

Any advice on where I am going wrong would be greatly appreciated.

Was it helpful?

Solution

Since the block wasn't given a name attribute by the module creator (as attribute is an alias, not a name), you have to do some work to find out what the actual name is.

You can do this by adding some code to the controller class for whatever page this is from:

$blockName = $resultPage->getLayout()->getChildName('root', 'thirdpartyvendor_success_message');
\Zend_Debug::dump($blockName);

The getChildName method takes the block's parent's name for first argument and the block's alias for second argument and returns the block's name. $resultPage is the page that gets returned by the execute method

Flush the cache and load the page and you should see a message at the top of the page that has the name, it will be something like 'content_schedule_block0'. Now you should remove the added code from the controller class.

Now that you have the block's name, you can reference it and change it's template from your modules layout file.

Another way is to change the block's template directly from an afterExecute plugin for the page's controller:

class Plugin
{

    public function afterExecute($subject, \Magento\Framework\View\Result\Page $resultPage)
    {
        $resultPage
            ->getLayout()
            ->getChildBlock('root', 'thirdpartyvendor_success_message')
            ->setTemplate('Mycompany_Module::custom_success_message.phtml');

        return $resultPage;
    }
}

This way may be preferable because if another block were added to the same parent, the dynamically generated 'content_schedule_block0' name could change. The drawback is that it's a bit weird to be changing a block template from a plugin.

OTHER TIPS

You should reference the block with the "name" of it, not alias of the Block.

Ex:

  <referenceBlock name="referenceBlockName">
    <action method="setTemplate">
       <argument name="template" xsi:type="string">Mycompany_Module::custom_success_message.phtml</argument>
    </action>
  </referenceBlock>

Above you should provide referenceBlockName, rather alias name. If block name is not available on third-party vendor module, just apply a name and try back.

Have you tried changing the module sequence order? In the ./app/code/MyCompany/MyModule/etc/module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MyCompany_MyModule" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

So the original module loads before the custom one? Sometimes it can help.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top