magently / module-content-setup
为 Magento 2 设计的 Magently 内容设置模块。
Requires
- php: >=7.1
- magento/framework: >=102.0.0
This package is auto-updated.
Last update: 2024-09-26 16:18:05 UTC
README
安装
要安装该模块,请运行以下命令
composer require magently/module-content-setup
php bin/magento setup:upgrade
用法
现在您可以在代码中使用该模块。在您的 UpgradeData / PatchData 类中注入 Magently\ContentSetup\Model\ContentSetupFactory
- 这里使用 Factory 是为了能够在项目中不同的模块中使用此模块,例如
public function __construct(\Magently\ContentSetup\Model\ContentSetupFactory $contentSetupFactory)
{
$this->contentSetupFactory = $contentSetupFactory;
}
然后通过传递您模块的名称从 Factory 创建一个对象,例如
/** @var \Magently\ContentSetup\Model\ContentSetup $setup */
$setup = $this->contentSetupFactory->create(['moduleName' => 'Magently_TestModule']);
从现在起,您可以使用该模块上传内容
$setup->setupPage('test_page');
$setup->setupBlock('test_block');
$setup->setupEmailTemplate('test_email');
$setup->setupVariable('test_variable', 'TEST VARIABLE');
Cms 页面、Cms 块和电子邮件模板必须放置在您模块的结构中: app/code/VendorName/ModuleName/Setup/Content/{COMPONENT_NAME}
,例如 app/code/Magently/TestModule/Setup/Content/Block/test_block.html
和 app/code/Magently/TestModule/Setup/Content/Block/test_block.php
文件将被用来创建/更新一个具有 test_block
标识符的块。将块的內容放在 .html
文件中。在 .php
文件中,将数据作为具有如 name
或 is_active
键的数组放置
// file: app/code/Magently/TestModule/Setup/Content/Block/test_block.php
<?php
return [
'title' => 'Magently Test Block',
'identifier' => 'test_block',
'is_active' => 1
];
按以下方式声明 Magento 变量,不使用任何文件
public function setupVariable(
string $code,
string $name = null,
string $plainValue = null,
string $htmlValue = null
)
更多信息
您可以在我们的 Magently 博客上了解更多关于该模块如何工作: https://magently.com/blog/add-magento-cms-blocks-programmatically
如果遇到问题或对改进模块的想法,请毫不犹豫地提交 pull request。