0

I'm trying to insert a template file as the content for the block I'm adding programmatically, but I'm not sure how this can be done. I've added the template file in view/frontend/templates/template-file.phtml

    public function __construct(
        BlockFactory $blockFactory,
        Block $blockResourceModel
    ) {
        $this->blockFactory = $blockFactory;
        $this->blockResourceModel = $blockResourceModel;
    }

    /**
     * {@inheritdoc}
     *
     * @return void
     * @throws \Exception
     */
    public function apply()
    {
        $cmsBlock = [
            'title' => 'Footer content',
            'identifier' => 'footer_content',
            'content' => <How to insert template file here?>,
            'is_active' => 1,
            'store_id' => [9]
        ];
        $block = $this->blockFactory->create();
        $block->setData($cmsBlock);
        $this->blockResourceModel->save($block);
    }

3 Answers 3

1

I ended up emulating the admin area and getting the contents of the template file within there.

/**
 * @return string
 * @throws \Exception
 */
private function getCmsBlockContent(): string
{
    $content = $this->state->emulateAreaCode(Area::AREA_ADMINHTML, function () {
        $layout = ObjectManager::getInstance()->get(LayoutInterface::class);
        return $layout->createBlock(Template::class)->setTemplate(self::TEMPLATE_FILE)->toHtml();
    });

    return (string) $content;
}
0

I guess you need to check how the block is binding with template file and how layout file works.

Check it out in the following article.

https://www.mageplaza.com/magento-2-module-development/view-block-layout-template-magento-2.html#step-3-create-block

1
  • I don't think this is possible in a DataPatch file. Commented Apr 21, 2022 at 11:42
0

Ended up with using Magento\Framework\Filesystem\Driver\File

    public function __construct(
        BlockFactory $blockFactory,
        Block $blockResourceModel,
        File $filesystemDriver
    ) {
        $this->blockFactory = $blockFactory;
        $this->blockResourceModel = $blockResourceModel;
        $this->filesystemDriver = $filesystemDriver;
    }

    /**
     * {@inheritdoc}
     *
     * @return void
     * @throws \Exception
     */
    public function apply()
    {
        $fileContents = $this->filesystemDriver->fileGetContents(
            'app/code/Vendor/Module/view/frontend/templates/template-file.phtml'
        );
        $cmsBlock = [
            'title' => 'Footer content',
            'identifier' => 'footer_content',
            'content' => $fileContents,
            'is_active' => 1,
            'store_id' => [9]
        ];
        $block = $this->blockFactory->create();
        $block->setData($cmsBlock);
        $this->blockResourceModel->save($block);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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