0

I can create a custom block then "import" another existing content, example:

{{block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form.phtml"}}

How can I import the content of my cms home page into a custom block?

1

Let's say I have a custom block Rkt\StackExchange\Block\Main defined at app/code/Rkt/StackExchange/Block/Main.php.

Now include this block within your CMS Page like this:

{{block class="Rkt\StackExchange\Block\Main" name="main" page_identifier="no-route" template="Rkt_StackExchange::form.phtml"}}

Important points to note here is, I included the page_identifier in the bock declaration. The page identifier is your URL key for the CMS page. Other than that I have specified the block type and block template. So let us define both of these files.

The Template File

File: app/code/Rkt/StackExchange/view/frontend/templates/form.phtml

<?php echo $block->getPage() ?>

As you can see, we are calling the getPage() method of block class to render the cms page content.

The Block Class

File: app/code/Rkt/StackExchange/Block/Main.php

<?php
namespace Rkt\StackExchange\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Cms\Model\PageFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Cms\Helper\Page as CmsPageHelper;
use Magento\Cms\Controller\Page\View as PageControllerView;
use Magento\Framework\App\ResponseInterface;

class Main extends Template
{
    protected $pageIdentifier;

    protected $storeManager;

    protected $pageFactory;

    protected $cmspageHelp;

    protected $request;

    protected $response;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param array $data
     */
    public function __construct(
        Context $context,
        PageFactory $pageFactory,
        StoreManagerInterface $storeManager,
        PageControllerView $request,
        ResponseInterface $response,
        CmsPageHelper $cmspageHelp,
        array $data = []
    ) {
        $this->pageFactory = $pageFactory;
        $this->storeManager = $storeManager;
        $this->cmspageHelp = $cmspageHelp;
        $this->request = $request;
        $this->response = $response;
        parent::__construct($context, $data);
    }

    public function getPage()
    {
        $page = $this->pageFactory->create();
        $pageId = $page->checkIdentifier(
            $this->getPageIdentifier(), 
            $this->storeManager->getStore()->getId()
        );
        if (!$pageId) {
            return '';
        }
        $resultPage = $this->cmspageHelp->prepareResultPage($this->request, $pageId);
        return $resultPage->renderResult($this->response);
    }

    public function getPageIdentifier()
    {
        return $this->pageIdentifier;
    }
    public function setPageIdentifier($identifier)
    {
        $this->pageIdentifier = $identifier;
    }
}

In the getPage() method, we are collecting the identifier, then using CMS Page helper to retrieve the content of the cms page.

Better Solution

Though the solution above given works for your requirement, I think we are making things complex here. I think the solution put forward by @magento68 seems to be a good solution here.

Or create a static block with the CMS page content that you wish to add in your current CMS page, then just invoke the static block in the CMS Page. This will be simple and easy to do.

1

Keep below xml in your cms_index_index.xml file and check in front, You have keep same block_identifier for both static block. You have to keep static block name as unique,

<referenceContainer name="content.bottom">
    <block class="Magento\Cms\Block\Block" name="block_identifier">
        <arguments>
            <argument name="block_id" xsi:type="string">block1</argument>
        </arguments>
    </block>

    <block class="Magento\Cms\Block\Block" name="block_identifier-second" after="-">
        <arguments>
            <argument name="block_id" xsi:type="string">block2</argument>
        </arguments>
    </block>
</referenceContainer>

Replace block1 and block2 with your static block id in above xml code.

OR

in admin panel go to Content->Pages and edit home page and if you created your static block from admin panel add following to the Content tab

{{block class="Magento\Cms\Block\Block" block_id="your_block_identifier"}}

and if you created a custom block using a module add following to home page content tab

{{block class="Vendor\ModuleName\Block\BlockName" template="Vendor_ModuleName::yourcustomblock.phtml"}} 

then save the changes and flush magento cache and deploy/delete static contents now refresh home page .

If this was not worked let me know

  • Thanks for your help. What's the "block_id" I can use to have all the existing contente from CMS Home page to be added to another block. That´s the question. – Luiz Junior Oct 1 '17 at 5:02
0

There are two way, you can add a block to home page:

Via Layout

Create cms_index_index.xml at app/design/frontend/{VendorName}/{themename}/Magebto_Cms/layout .

then ad below code at in this files:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">4
   <body>
    <referenceContainer name="content">
            <block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form.phtm">
                <container name="form.additional.info" label="Form Additional Info"/>
            </block>
        </referenceContainer>
    <body/>
</page>

Via admin CMS Page:

Go to admin panel go to Content->Pages ,select your home page and edit home page to the Content tab.

{{block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form.phtml"}}

In this way, you can add any block to home.

Just need to changes from

class="Magento\Contact\Block\ContactForm"

to

class="[Your_Customer_Block_Class_name]"

Also,need to change template file:

template="Magento_Contact::form.phtml"

to

template="{VendorName}_{ModuleName}::{templatefilelocatin}/{filename}.phtml"

  • Thanks, but that's not what I have asked. Thank you anyway. – Luiz Junior Oct 1 '17 at 5:00
  • Can you please tell me what you want? what you mean by import? – Amit Bera Oct 1 '17 at 5:25
  • Hi Amit, my cms home page is indeed a page (cms/page/view/page_id/2). What I want to do is create a block with the same content that already exists on that page. The reason I want that is because I added a Category named "home page" but I want it to redirect to home page instead of listing produts. So I would set that category to display only a custom block. Tks! – Luiz Junior Oct 1 '17 at 5:32
-1

If you have the homepage contents stored in a static block, say "block_homepage_contents", you can include that CMS static block in to your custom block.

{{block class="Magento\Cms\Block\Block" block_id="block_homepage_contents"}}
  • That´s the question. What's the "block_id" I can use to have all the existing contente from CMS Home page to be added to another block. – Luiz Junior Oct 1 '17 at 5:01
  • this is the way you can add static cms block. In the question custom block i think dynamic block that exist in module – HaFiz Umer Nov 6 at 7:46

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.