0

我正在以编程方式创建页面并将内容插入到内容块中,但在创建后,我无法通过作曲家对其进行编辑(因为它不是 core_page_type_composer_control_output,而是常规内容块)。有没有办法以编程方式将块添加到页面并让它在 Composer 中运行良好?

我正在使用的相关代码:

$page = Page::getByPath('/articles/xxx');
$block = BlockType::getByHandle('content');
$data = array(
    'content' => 'the content',
);
$page->addBlock($block, 'Main', $data);
4

1 回答 1

0

为此,我将设置页面类型,就像您使用 composer 创建页面一样。
(将内容块添加到作曲家和页面类型的标准输出)

创建页面后,您会注意到与作曲家链接的内容块出现在页面上,但完全是空的。然后我会更新块,而不是尝试在“主要”区域创建一个新块。

代码示例(未测试):

$page = Page::getByPath('/articles/xxx');
$blocks = $page->getBlocks('Main');
if(!empty($blocks)){
  foreach($blocks as $block){
    //check if the blocktype is content
    if($block->getBlockTypeHandle() == 'content'){
      //we are pretty sure this is the content block in the main area
      //because it's the only content block present... updating the block content
      $data = array(
                'content' => 'the content',
              );
      $block->update($data)
    }
  }
}
于 2016-12-13T21:05:43.253 回答