1

as the title suggests

I'm creating a module that programmatically installs pages on cms_page, my problem arises when I want to load the contents of a phtml file and pass it as html into the array to fill the content index ...

I have not very clear the path of where to put the phtml file and how to recall it indicating that path ....

example of the current structure

app
 | - code
 |    |- MyVendor
 |        |- MyModule
 |            |- registration.php
 |            |- etc
 |                |- module.xml
 |            |- Setup
 |                 |- InstallData.php
 |            |- view
 |                 |- frontend
 |                      |- templates
 |                           |- homepage.phtml

is the above path right? who can write me the path of where to insert the phtml files and how to recall them?

namespace MyVendor\MyModule\Setup;
use Magento\Cms\Model\PageFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $pageFactory;

public function __construct(PageFactory $pageFactory)
{
    $this->_pageFactory = $pageFactory;
}

/**
 * Installs data for a module
 *
 * @param ModuleDataSetupInterface $setup
 * @param ModuleContextInterface $context
 * @return void
 * @throws \Exception
 */
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    // Load cms page by identifier
    $cmsPage = $this->_pageFactory->create()->load('home-page', 'identifier');

    $content = '<div id="something">
                {{block class="Magento\Framework\View\Element\Template" name="home-page" template="app/code/MyVendor/MyModule/view/frontend/templates/homepage.phtml" }}
                </div>';


    // Create CMS Page
    if (!$cmsPage->getId()) {
        $cmsPageData = [
            'title' => 'Custom cms page', // cms page title
            'page_layout' => '1column', // cms page layout
            'meta_keywords' => 'Page keywords', // cms page meta keywords
            'meta_description' => 'Page description', // cms page description
            'identifier' => 'home-page', // cms page url identifier
            'content_heading' => 'Custom Home page', // Page heading
            'content' => $content, // page content
            'is_active' => 1, // define active status
            'stores' => [0], // assign to stores
            'sort_order' => 0 // page sort order
        ];

        $this->_pageFactory->create()->setData($cmsPageData)->save();
    }
}
}

After apply the suggested changes RETURN ERROR:

main.CRITICAL: Invalid template file: 'app/code/MyVendor/MyModule/view/frontend/templates/homepage.phtml' in module: '' block's name: 'magento\framework\view\element\template_0' {"exception":"[object] (Magento\\Framework\\Exception\\ValidatorException(code: 0): Invalid template file: 'app/code/MyVendor/MyModule/view/frontend/templates/homepage.phtml' in module: '' block's name: 'magento\\framework\\view\\element\\template_0' at /var/www/html/vendor/magento/framework/View/Element/Template.php:277)"} []
  • app/code/MyVendor/MyModule/view/frontend/templates/mypage.phtml – temper Oct 1 at 12:05
  • return error Invalid template file – Orazio D'Arrigo Oct 2 at 15:04
0

You need to write Phtml in path:

**app/code/MyVendor/MyModule/view/frontend/templates/mypage.phtml**

And also recommended this link for further Problem.

0

Did you try letting the cms logic do the work.

Setup script as demonstrated in other answer.

Content something like this

<div id="something">
    {{block class="Magento\Framework\View\Element\Template" name="test" template="path/to/my/file.phtml" }}
</div>
  • Thank for the suggestion, now have used inside the InstallData.php your code but when i load the page return me this error: Invalid template file: 'app/code/MyVendor/MyModule/view/frontend/templates/homepage.phtml' in module: '' block's name: 'magento\framework\view\element\template_0' – Orazio D'Arrigo Oct 2 at 9:00

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.