如何在Magento 2中以编程方式添加CMS块?


13

我需要通过安装/升级脚本添加CMS块。我已经弄清楚了如何添加“普通” CMS页面,如下面的脚本所示。但是,由于我找不到在Magento 2的代码中添加CMS块的任何方法,因此无论是在Google还是在这里,我都很困惑。

namespace [Vendor]\[Module]\Setup;

use Magento\Cms\Model\Page;
use Magento\Cms\Model\PageFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * Page factory.
     *
     * @var PageFactory
     */
    private $pageFactory;

    /**
     * Init.
     *
     * @param PageFactory $pageFactory
     */
    public function __construct(PageFactory $pageFactory)
    {
        $this->pageFactory = $pageFactory;
    }

    /**
     * Upgrade.
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '0.0.1') < 0) {
            $testPage = [
                'title' => 'Test page title',
                'identifier' => 'test-page',
                'stores' => [0],
                'is_active' => 1,
                'content_heading' => 'Test page heading',
                'content' => 'Test page content',
                'page_layout' => '1column'
            ];

            $this->pageFactory->create()->setData($testPage)->save();
        }

        $setup->endSetup();
    }
}

我知道我不需要$testPage数组中定义的所有值,因此我将其简化为以下内容:

$testPage = [
    'title' => 'Test block title',
    'identifier' => 'test-block',
    'stores' => [0],
    'is_active' => 1
    'content' => 'Test block content'
];

有谁知道我需要进行哪些更改才能将该测试页转换为测试块?

注意:我的脚本基于位于的Magento 2 CMS模块中的安装数据脚本vendor/magento/module-cms/Setup/InstallData.php


这不是“ CMS阻止”,而是“ CMS页面” ..误导性的标题
。– OZZIE

@OZZIE不是。如果您已经阅读了说明,您将会知道上面的代码确实是要添加一个页面,但是问题是将其更改为CMS块安装程序。

Answers:


20

将此添加到您的班级:

private $blockFactory;

并使您的构造函数如下所示:

public function __construct(
    PageFactory $pageFactory,
    \Magento\Cms\Model\BlockFactory $blockFactory
  )
{
    $this->pageFactory = $pageFactory;
    $this->blockFactory = $blockFactory;
}

您甚至可以删除$pageFactory不再需要的依赖项。

然后替换:

$testPage = [
    'title' => 'Test page title',
    'identifier' => 'test-page',
    'stores' => [0],
    'is_active' => 1,
    'content_heading' => 'Test page heading',
    'content' => 'Test page content',
    'page_layout' => '1column'
];

$this->pageFactory->create()->setData($testPage)->save();

$testBlock = [
    'title' => 'Test block title',
    'identifier' => 'test-block',
    'stores' => [0],
    'is_active' => 1,
];
$this->blockFactory->create()->setData($testBlock)->save();

2
我内心的某些事情告诉我,我本可以自己弄清楚的。很好,很容易回答。非常感谢你。PS:我想投票赞成你,但我的名声让我无法接受。

简单但有效。@KeesHak我为您投票:P
7ochem '16

@KeesHak最好使用数据接口(BlockInterface)和存储库(BlockRepositoryInterface)代替工厂。
Vasilii Burlacu

@VasiliiBurlacu你是对的。我在1.5年前写了这篇文章,当时存储库还没有完成。
马里斯(Marius)

13

您可以尝试以下解决方案:

向构造函数添加2个类:

/**
 * @var \Magento\Cms\Model\BlockFactory
 */
protected $blockFactory;

/**
 * @var \Magento\Cms\Model\BlockRepository
 */
protected $blockRepository;

/**
 * @param PageFactory $resultPageFactory
 * @param \Magento\Cms\Model\BlockFactory $blockFactory
 * @param \Magento\Cms\Model\BlockRepository $blockRepository
 */
public function __construct(
    PageFactory $resultPageFactory,
    \Magento\Cms\Model\BlockFactory $blockFactory,
    \Magento\Cms\Model\BlockRepository $blockRepository
) {
    $this->resultPageFactory = $resultPageFactory;
    $this->blockFactory = $blockFactory;
    $this->blockRepository = $blockRepository;
}

并以以下方式使用它们:

$data = [
    'title' => 'Test block title',
    'identifier' => 'test-block',
    'stores' => ['0'],
    'is_active' => 1,
    'content' => 'Test block content'
];
$newBlock = $this->blockFactory->create(['data' => $data]);
$this->blockRepository->save($newBlock);

该代码正在运行。这是此块的结果:

在此处输入图片说明


我还实现了块存储库功能来保存块。确实似乎有更好的做法。谢谢。

0

如果您的模块需要升级,请使用UpgradeData.php并执行以下步骤。

  1. 增加module.xml中的setup_version
  2. 转到安装目录并创建UpgradeData.php
  3. 如下导入其依赖项,

    namespace Vendor\YourModule\Setup;
    
    use Magento\Framework\Setup\UpgradeDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    use Magento\Cms\Model\BlockFactory;
    
    class UpgradeData implements UpgradeDataInterface {
    
    private $blockFactory;
    
    public function __construct(LoggerInterface $logger, BlockFactory $blockFactory) {
        $this->blockFactory = $blockFactory;
    }
    
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
    
        $setup->startSetup();
    
        if (version_compare($context->getVersion(), '1.1.0') < 0) {
            try{
              $staticBlockInfo = [
                        'title' => 'Title of Sample block',
                        'identifier' => 'sample_block',
                        'stores' => ['0'],
                        'is_active' => 1,
                        'content' => 'This is the sample block content'
                      ];
                $this->blockFactory->create()->setData($staticBlockInfo)->save();
            }catch (Exception $e){
                echo $e->getMessage();
            }
         }
    
         $setup->endSetup();
       }
    }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.