Question

I have a third-party module and I'm trying to inject a block of my own (a CMS block) between two of their blocks. I'm doing something wrong because I can get my block to appear at the very end or the very beginning but can't get it in the location I want it.

Here's their layout:

<?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">
    <head>
        <script src="https://www.google.com/recaptcha/api.js" src_type="url"/>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="TemplateMonster\Blog\Block\Post\View" name="blog.post" template="TemplateMonster_Blog::post/view.phtml">
                <block class="Magento\Framework\View\Element\Template"
                       name="blog.social.sharing"
                       template="TemplateMonster_Blog::post/social_sharing/social_sharing.phtml"
                       ifconfig="tm_blog/social_sharing/general/enabled">
                    <block class="TemplateMonster\Blog\Block\Post\SocialSharing\AddThis\Icons" name="blog.social.sharing.icons" as="social_sharing_icons" />
                </block>
                <block class="TemplateMonster\Blog\Block\Post\View\Comments" name="blog.post.comments" as="comments" template="TemplateMonster_Blog::post/view/comments.phtml" />
                <!-- I want my block to appear here -->
                <block class="TemplateMonster\Blog\Block\Post\View\Products" name="blog.post.products" as="products"  template="TemplateMonster_Blog::post/view/products.phtml" />
                <block class="TemplateMonster\Blog\Block\Post\View\Posts" name="blog.post.posts" as="posts"  template="TemplateMonster_Blog::post/view/posts.phtml" />
            </block>
        </referenceContainer>
        <referenceContainer name="sidebar.additional">
            <!-- ... SNIP ... -->
        </referenceContainer>
        <move element="page.main.title" destination="main" before="-" />
    </body>
</page>

And in my theme I use the following in the equivalently named location:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <!--<referenceBlock name="blog.post">-->
                <block class="Magento\Cms\Block\Block" name="blog.engagement" before="blog.post.products">
                    <arguments>
                        <argument name="block_id" xsi:type="string">blog-engagement</argument>
                    </arguments>
                </block>
            <!--</referenceBlock>-->
        </referenceContainer>
    </body>
</page>

So regardless of what I might use for "before" it always appears at the end. I'm thinking that it's because the block I'm trying to target is itself contained inside the "blog.post" block. The commented out referenceBlock line was me trying to get there but if I uncomment that element, my block doesn't appear on the page at all. I get the same result (element does not appear on the page) if I try to reference the block without the container.

What am I doing wrong?

Was it helpful?

Solution 2

After some research I don't think that I can do what I wanted to do; the template referenced in the inner block (TemplateMonster_Blog::post/view.phtml) has access to all of the blocks inside it but determines their order based on the contents of the view. So there was no way to insert a CMS block inside or between other elements.

My solution was to override the template and put in my own link in place, i.e.:

<?php echo $this->getChildHtml('comments'); ?>
<?php echo $this->getLayout()->createBlock(\Magento\Cms\Block\Block::class)->setBlockId('blog-engagement')->toHtml();?>
<?php echo $this->getChildHtml('products'); ?>

OTHER TIPS

You can use before="-". it will display block beginning of content

<block class="Magento\Cms\Block\Block" name="blog.engagement" before="-"> 
<arguments> 
<argument name="block_id" xsi:type="string">blog-engagement</argument> 
</arguments>
 </block>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top