I'm trying to use the Magento\Framework\View\Element\Text block, in a custom view but the output is not rendering.

This is my layout file

<?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">
        <referenceContainer name="content">
            <block name="custom.test.block" template="Vulpea_Frontend::test.phtml" />
            <block name="some.custom.text.block" type="Magento\Framework\View\Element\Text">
                <action method="setText">
                    <argument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="text" xsi:type="string">Some Custom Text</argument>

My debugging so far:

  • The layout file is correctly used. The page is accessible in storefront and the other block is rendering just fine.
  • The Text block is added to the layout. I can see it in the $_blocks property of the Magento\Framework\View\Layout class. The text is there as well as a property of the block.
  • For some whatever reason the _toHtml() method of the my custom text block is never called. It is however called for other text blocks on the page store.links for example.

My Questions (no need to answer to all of them):

  • Am I wrong in expecting this block to render automatically? Does this block need a template? Should I put this block in a parent block and call it manually?

How should I proceed in fixing or debugging this?

BTW the whole module is visible here


You need to change type to class:

<block name="some.custom.text.block" class="Magento\Framework\View\Element\Text">

I also recommend not using the action tag as it has been deprecated, use arguments instead:

    <argument translate="true" name="text" xsi:type="string">Some Custom Text</argument>

See Magento 2: How to add text within container via XML for some other methods.

  • That was quick. I can't believe I missed that. Tank you for the best practice tip as well. – vitoriodachef Jun 6 '19 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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