Magento2 – Custom Block with CMS Home Page Content

magento2

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?

Best Answer

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.