1

I am new to Magento, but building my first site in Magento 2. I have created a block in the CMS, and added it to my override/base/default.xml file like so;

<referenceContainer name="footer-container">
            <container name="footer" as="footer" label="Page Footer" htmlTag="div" htmlClass="footer content">
                <block class="Magento\Store\Block\Switcher" name="store_switcher" as="store_switcher" template="switch/stores.phtml"/>
                <block class="Magento\Framework\View\Element\Html\Links" name="footer_links">
                    <arguments>
                        <argument name="css_class" xsi:type="string">footer links</argument>
                    </arguments>
                </block>
                <block class="Magento\Cms\Block\Block" name="footer_left">
                    <arguments>
                        <argument name="css_class" xsi:type="string">footer_left</argument>
                    </arguments>
                </block>
            </container>
        </referenceContainer>

I have added my new block inside the footer-container under the footer_links block. Its Footer_left block

Its not showing up on the front end, any ideas why please?

Thanks in advance

0

You've added a Static Block to your layout, but you have not provided which static block should be rendered. You need to add the block_id:

<block class="Magento\Cms\Block\Block" name="footer_left">
    <arguments>
        <argument name="css_class" xsi:type="string">footer_left</argument>
        <argument name="block_id" xsi:type="string">[block_name]</argument>
    </arguments>
</block>

Where [block_name] is the code of your block (I'm guessing that's footer_left in your case).

Haven't tried it yet though...

The reason why this works is because with <block class="Magento\Cms\Block\Block" name="footer_left"> you're basically adding a new block to the layout of type "Magento\Cms\Block\Block" with the name "footer_left". Note that this the name of the block in the layout, not the name of the block that you've given in the admin.

The name matters in templates when you see something like this:

echo $block->getChildHtml('child_block_name');

This is layout and has nothing to do with CMS Blocks. Now, with the arguments you are configuring your block:

<arguments>
    <argument name="css_class" xsi:type="string">footer_left</argument>
    <argument name="block_id" xsi:type="string">[block_name]</argument>
</arguments>

These arguments are added to the $data-parameter in the constructor of the block class. So if your look at \Magento\Cms\Block\Block::__construct():

public function __construct(
    \Magento\Framework\View\Element\Context $context,
    \Magento\Cms\Model\Template\FilterProvider $filterProvider,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Cms\Model\BlockFactory $blockFactory,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->_filterProvider = $filterProvider;
    $this->_storeManager = $storeManager;
    $this->_blockFactory = $blockFactory;
}

You're basically instantiating a new block class somewhat like this:

public function __construct(
    \Magento\Framework\View\Element\Context $context,
    \Magento\Cms\Model\Template\FilterProvider $filterProvider,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Cms\Model\BlockFactory $blockFactory,
    array $data = [
        'css_class' => 'footer_left',
        'block_id'  => '[block_name]'
    ]
) {
    parent::__construct($context, $data);
    $this->_filterProvider = $filterProvider;
    $this->_storeManager = $storeManager;
    $this->_blockFactory = $blockFactory;
}

Now, I will not go in to deep on the magic getters and setters of Magento, but since you've set values for $data['css_class'] and $data['block_id'] using your layout XML, the following block methods will return those values:

$block->getCssClass() // will return 'footer_left'
$block->getBlockId()  // will return '[block_name]'

If you look at \Magento\Cms\Block\Block::_toHtml() you'll see how getBlockId() is used.

  • Wow that actually worked! Thanks very much. I am a little confused how this works, as i already thought i was rendering the block with the first argument there name="css_class". I have commented out that arguement and it still works! What does css_class do? – Jay Jun 22 '17 at 14:03
  • I'll update my answer for you – Giel Berkers Jun 22 '17 at 14:12

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.