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)"} []