0

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.

2
  • Just override file in Your Theme it will solve issue
    – Jackson
    Commented Apr 26, 2017 at 4:11
  • Unfortunately overriding inside the theme isn't an option for this project.
    – Eirik
    Commented Apr 26, 2017 at 4:14

3 Answers 3

1

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.

1
  • This got me there overriding the template in afterExecute. Ideally it would have been better if I could have just overridden the theme in xml but as you said, I didn't have access to the name and I couldn't guarantee the third party developer doesn't change the naming convention after the fact.
    – Eirik
    Commented Apr 26, 2017 at 13:32
1

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.

1
  • I had tried adding the block name previously and still had the same result. No template override. I've gone with Aaron Allens method of overriding via a plugin. Thank you for taking the time to provide a suggestion though.
    – Eirik
    Commented Apr 26, 2017 at 13:35
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.