my task sounds like : "Using a data patch to add the block into cms page"
i have
data patch cms block
class CmcBlock implements DataPatchInterface
{
const CMC_BLOCK = 'first_cmc_block';
private ModuleDataSetupInterface $moduleDataSetup;
private BlockFactory $block;
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
BlockFactory $blockFactory,
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->block = $blockFactory;
}
public static function getDependencies()
{
return [
DependFromCmsPage::class,
];
}
public function getAliases()
{
return [];
}
public function apply()
{
$this->block->create()
->setTitle(self::CMC_BLOCK)
->setIdentifier(self::CMC_BLOCK)
->setIsActive(true)
->setContent($this->cmcBlockContent())
->setStores([Store::DEFAULT_STORE_ID])
->save();
}
public function cmcBlockContent(): string
{
return '<div>first CMC block content<a href="gogo">content</a></div>';
}
}
data patch cms page
class CmsPage implements DataPatchInterface
{
const CMS_PAGE = 'first_cms_page';
/**
* @var ModuleDataSetupInterface
*/
private ModuleDataSetupInterface $moduleDataSetup;
/**
* @var PageFactory
*/
private PageFactory $pageFactory;
/**
* @var PageRepositoryInterface
*/
private PageRepositoryInterface $pageRepository;
/**
* @var State
*/
private State $state;
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
PageFactory $pageFactory,
PageRepositoryInterface $pageRepository,
State $state,
LoggerInterface $logger,
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->pageFactory = $pageFactory;
$this->pageRepository = $pageRepository;
$this->state = $state;
$this->logger = $logger;
}
/**
* @return string[]
*/
public static function getDependencies()
{
return[];
}
/**
* @return string[]
*/
public function getAliases()
{
return [];
}
public function apply()
{
try {
$this->state->emulateAreaCode(
Area::AREA_ADMINHTML,
[$this, 'setupCustomCmsPage']
);
} catch (Exception $e) {
$this->logger->error(
'Error when Saving settings for patch CustomCmsPage: ' . $e->getMessage()
);
}
return $this;
}
/**
* This patch is to create Custom Cms Page
*/
public function setupCustomCmsPage()
{
$cmsPage = $this->createPage()->setData($this->getPage());
$this->pageRepository->save($cmsPage);
}
/**
* Create page instance.
*/
private function createPage()
{
return $this->pageFactory->create();
}
/**
* Get Page Config
* @return array
*/
private function getPage(): array
{
return [
'title' => 'Test CMS Page',
'page_layout' => '1column',
'meta_keywords' => 'test meta keywords',
'meta_description' => 'test meta description',
'identifier' => self::CMS_PAGE,
'content_heading' => '',
'content' => $this->getCmsPageHtml(),
'layout_update_xml' => '',
'is_active' => 1,
'stores' => [Store::DEFAULT_STORE_ID],
'sort_order' => 0
];
}
/**
* Get Custom CMS page html
* @return string
*/
private function getCmsPageHtml(): string
{
return 'HTML You can set here Html Content HTML';
}
}
how i can insert cms block to cms page pragrammatically?