sifuen / module-upgradable-content
使用UpgradeData脚本升级CMS内容
Requires
- magento/framework: 100.0.*|100.1.*|101.0.*
This package is auto-updated.
Last update: 2022-11-04 11:05:01 UTC
README
此Magento 2模块允许您使用UpgradeData脚本升级您的CMS块和页面。
安装
您可以使用composer安装此模块
composer require sifuen/module-upgradable-content
或者,您可以克隆此存储库并将其放置在app/code/Sifuen/UpgradableContent/中。
php bin/magento module:enable Sifuen_UpgradableContent
如何使用
您的UpgradeData脚本应注入Sifuen\UpgradableContent\Model\ContentUpgrader。在您的构造函数中,您应使用$this->contentUpgrader->setContentModule([your module name])初始化ContentUpgrader。这是用于查找您的CMS内容文件。
初始化
假设您在创建UpgradeData文件中创建的模块名称为Sifuen_CmsTest,您将调用setContentModule('Sifuen_CmsTest')以告知ContentUpgrader该模块是您可以找到CMS内容文件的位置。
默认情况下,CMS内容文件从[your module directory]/Setup/content/[version]/[pages/blocks]/[identifier].html读取。要更改此设置,请参阅高级初始化
有关初始化ContentUpgrader的其他方式,请参阅高级初始化
use Sifuen\UpgradableContent\Model\ContentUpgrader; class UpgradeData implements UpgradeDataInterface { /** * @var ContentUpgrader */ private $contentUpgrader; /** * UpgradeData constructor. * @param ContentUpgrader $contentUpgrader */ public function __construct( ContentUpgrader $contentUpgrader ) { $this->contentUpgrader = $contentUpgrader; // Set the current module we are in so the content upgrader // can find our content files $this->contentUpgrader->setContentModule('Sifuen_CmsTest'); }
升级内容
您可以对ContentUpgrader实例访问两个主要方法。
$this->upgradeCmsPages($version, $identifiers)
$this->upgradeCmsBlocks($version, $identifiers)
$version是您要升级到的模块版本。
$identifiers可以是两种东西
- 页面/块标识符数组
- 数组数组,其中每个元素的索引是要更新/创建的页面/块的标识符,值是页面/块数据(除内容外)。
如果您正在创建CMS内容,则通常使用$identifiers的第二种版本,如果您正在更新CMS内容,则使用第一种版本。
示例
/** * @param ModuleDataSetupInterface $setup * @param ModuleContextInterface $context * @throws \Magento\Framework\Exception\LocalizedException */ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { if (version_compare($context->getVersion(), '1.0.1', '<')) { /** * In this instance, we are creating a new CMS page with the identifier * 'example-page-test'. The array of page data is added to the Page model * using addData() */ $this->contentUpgrader->upgradePages('1.0.1', [ 'examplepage-test' => [ 'title' => 'Example Page', 'page_layout' => '1column', 'content_heading' => 'Example Page' ] ]); } if (version_compare($context->getVersion(), '1.0.2', '<')) { /** * Now we're upgrading an existing page named 'examplepage-test'. Only * the content will be updated to what the HTML file contains. */ $this->contentUpgrader->upgradePages('1.0.2', ['examplepage-test']); } if (version_compare($context->getVersion(), '1.0.3', '<')) { /** * This will create a new CMS block named 'exampleblock-test' with the title * 'Example Block Title'. */ $this->contentUpgrader->upgradeBlocks('1.0.3', [ 'exampleblock-test' => [ 'title' => 'Example Block Title' ] ]); } if (version_compare($context->getVersion(), '1.0.4', '<')) { /** * This will update the CMS block 'exampleblock-test' with the newest content from the HTML file */ $this->contentUpgrader->upgradeBlocks('1.0.4', ['exampleblock-test']); } }
ContentUpgrader将读取的文件是
- app/code/Sifuen/CmsTest/Setup/content/1.0.1/pages/examplepage-test.html
- app/code/Sifuen/CmsTest/Setup/content/1.0.2/pages/examplepage-test.html
- app/code/Sifuen/CmsTest/Setup/content/1.0.3/blocks/exampleblock-test.html
- app/code/Sifuen/CmsTest/Setup/content/1.0.4/blocks/exampleblock-test.html
高级初始化
在Sifuen\UpgradableContent\Model\ContentUpgrader上可以覆盖一些构造函数参数。请参阅以下di.xml示例,以查看可覆盖的内容。
如果您在ContentUpgrader上设置了contentDirectory,则忽略contentModuleName和moduleContentFolder。contentDirectory的目的是完全覆盖模块查找内容文件的方式。
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Sifuen\UpgradableContent\Model\ContentUpgrader"> <arguments> <!-- The full path where you can find your CMS content files --> <argument name="contentDirectory" xsi:type="string">/var/somewhere/else/completely</argument> <!-- The module name where to find your CMS content files --> <argument name="contentModuleName" xsi:type="string">Sifuen_ModuleName</argument> <!-- Where in the module it can find your CMS content files --> <argument name="moduleContentFolder" xsi:type="string">Setup/content</argument> <!-- The file extension your CMS content files will have --> <argument name="contentFileExtension" xsi:type="string">.html</argument> </arguments> </type> </config>
您也可以在UpgradeData脚本的构造函数中手动设置这些参数。
/** * UpgradeData constructor. * @param ContentUpgrader $contentUpgrader */ public function __construct( ContentUpgrader $contentUpgrader ) { $this->contentUpgrader = $contentUpgrader; // The full path where you can find your CMS content files $this->contentUpgrader->setContentDirectory('/var/somewhere/else/completely'); // The module name where to find your CMS content files $this->contentUpgrader->setContentModule('Sifuen_CmsTest'); // Where in the module it can find your CMS content files $this->contentUpgrader->setModuleContentFolder('Setup/content'); // The file extension your CMS content files will have $this->contentUpgrader->setContentFileExtension('.html'); }