rocketweb / module-content-update
用于使用数据脚本来创建和更新静态内容的模块。
1.1.0
2019-02-28 10:05 UTC
This package is auto-updated.
Last update: 2024-09-28 23:21:50 UTC
README
该模块负责使用数据脚本创建和更新静态内容。
安装
使用Composer安装
$ composer require rocketweb/module-content-update
$ bin/magento module:enable RocketWeb_ContentUpdate
$ bin/magento setup:upgrade
手动安装:将模块内容下载到 app/code/RocketWeb/ContentUpdate
配置
在 app/code 目录中创建新的模块 ProjectNamespace/ContentUpdate,使用以下步骤
在以下步骤中将 ProjectNamespace 替换为项目或供应商命名空间
- 创建
composer.json
{
"name": "projectNamespace/module-content-update",
"description": "Module for creating and updating static content using data scripts.",
"type": "magento2-module",
"version": "1.0.0",
"license": "GPL-3.0",
"authors": [
{
"name": "Company Name",
"email": "company@email.com"
}
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Projectnamespace\\UpgradeData\\": ""
}
}
}
- 创建
registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'ProjectNamespace_ContentUpdate', __DIR__ );
- 创建
etc/module.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="ProjectNamespace_ContentUpdate" setup_version="1.0.0"> <sequence> <module name="RocketWeb_ContentUpdate"/> </sequence> </module> </config>
-
将
vendor/rocketweb/module-content-update/Setup/UpgradeData克隆到app/code/ProjectNamespace/ContentUpdate/Setup/UpgradeData.php -
打开
Setup/UpgradeData.php并替换
namespace RocketWeb\ContentUpdate\Setup
为
namespace ProjectNamespace\ContentUpdate\Setup
-
清除UpgradeData.php中的示例函数并添加自己的(参见下一节中的参考)
-
运行
bin/magento module:enable ProjectNamespace/ContentUpdate
bin/magento setup:upgrade
使用方法
添加更新函数并触发它们
- 打开
ProjectNamespace_ContentUpdate/etc/module.xml - 将 setup_version 属性从 x.y.z 更改为 x.y.z++,例如,1.0.9 更改为 1.0.10
- 打开
ProjectNamespace_ContentUpdate/Setup/UpgradeData.php - 滚动到文件底部
- 在闭合大括号之前添加您的函数(使用唯一名称,如 createUIPage)。以下是如何创建和更新各种元素的参考。
- 创建函数完成后,向上滚动并找到 $setup->endSetup();
- 该行之前的最后一条记录应类似于
if (version_compare($context->getVersion(), '1.0.9') < 0) {
$this->someFunction($helperSetup);
}
- 复制此条目并更新设置版本号(与module.xml中的相匹配)和函数名称(到最近创建的一个)
- 完成后保存文件并运行
bin/magento setup:upgrade - 转到您创建的页面/块以确认其正常工作
如果您需要调整模块的 setup_version 号码,可以在修改 setup_module 表中的数据库条目时进行。确保在运行
magento setup:uprade之前恢复 schema_version 和 data_version。请注意,您只能在提交之前通过数据库进行更改。
更新函数
创建新的CMS页面
public function createNewCmsPage($helperSetup) { $storeId = $helperSetup->getStoreId('admin'); $stores = [$storeId]; // Or $stores = = [$storeId, .. ]; to assign the page to more than one store $content = <<<EOD <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> EOD; $layoutUpdateXml = <<<EOD <referenceContainer name="content"></referenceContainer> EOD; $data = [ 'title' => 'Lorem Ipsum', 'page_layout' => '1column', 'meta_title' => '', 'meta_keywords' => '', 'meta_description' => '', //'identifier' => 'other-lorem-ipsum', // Only to update old identifier if we want to 'content_heading' => '', 'content' => $content, 'is_active' => Page::STATUS_ENABLED, 'sort_order' => 0, 'layout_update_xml' => $layoutUpdateXml ]; $helperSetup->createCmsPage('lorem-ipsum', $data, $stores); }
更新现有的CMS页面
public function updateExistingCmsPage($helperSetup) { $storeId = $helperSetup->getStoreId('admin'); $data = [ 'title' => 'Lorem Ipsum Changed Title', 'identifier' => 'other-lorem-ipsum', // This time we change identifier 'layout_update_xml' => '', ]; $helperSetup->updateCmsPage('lorem-ipsum', $data, $storeId); }
删除现有的CMS页面
public function deleteExistingCmsPage($helperSetup) { $storeId = $helperSetup->getStoreId('admin'); $helperSetup->deleteCmsPage('other-lorem-ipsum', $storeId); }
创建新的CMS块
public function createNewCmsBlock($helperSetup) { $storeId = $helperSetup->getStoreId('admin'); $stores = [$storeId]; // Or $stores = = [$storeId, .. ]; to assign the block to more than one store $content = <<<EOD <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p> EOD; $data = [ 'title' => 'Lorem Ipsum', 'content' => $content, 'is_active' => Block::STATUS_ENABLED ]; $helperSetup->createCmsBlock('dolor-sit-amet', $data, $stores); }
更新现有的CMS块
public function updateExistingCmsBlock($helperSetup) { $storeId = $helperSetup->getStoreId('admin'); $data = [ 'is_active' => Block::STATUS_DISABLED, 'identifier' => 'dolor-sit-amet-other', // This time we change identifier ]; $helperSetup->updateCmsBlock('dolor-sit-amet', $data, $storeId); }
删除现有的CMS块
public function deleteExistingCmsBlock($helperSetup) { $storeId = $helperSetup->getStoreId('admin'); $helperSetup->deleteCmsBlock('dolor-sit-amet-other', $storeId); }
创建新的配置
public function createConfiguration($helperSetup) { // Set value to default Scope, to admin store implicitly $helperSetup->saveConfigValue( 'rw_lorem_ipsum/general/hello_world', 'Hello!' ); // Set value to Scope 'store', to store with id 1 $helperSetup->saveConfigValue( 'rw_lorem_ipsum/general/hello_usa', 'Hi!', ScopeInterface::SCOPE_STORE, Store::DISTRO_STORE_ID ); // Can use to get store id // $storeId = $helperSetup->getStoreId('my store code'); $websiteId = $helperSetup->getWebsiteId('base'); if ($websiteId) { // Set value to Scope 'website', to website with id $websiteId $helperSetup->saveConfigValue( 'rw_lorem_ipsum/general/we_said_hello', '1', ScopeInterface::SCOPE_WEBSITES, $websiteId ); } }
更新现有的配置
public function updateConfiguration($helperSetup) { // Get value to Scope 'store', to store with id 1 $usa = $helperSetup->getConfigValue( 'rw_lorem_ipsum/general/hello_usa', ScopeInterface::SCOPE_STORE, $helperSetup->getStoreCode(Store::DISTRO_STORE_ID) ); $usa .= " Lorem Ipsum"; // Set value to Scope 'store', to store with id 1 $helperSetup->saveConfigValue( 'rw_lorem_ipsum/general/hello_usa', $usa, ScopeInterface::SCOPE_STORE, Store::DISTRO_STORE_ID ); $websiteId = $helperSetup->getWebsiteId('base'); if ($websiteId) { // Set value to Scope 'website', to website with id $websiteId /*$is = $helperSetup->isConfigSetFlag( 'rw_lorem_ipsum/general/we_said_hello', ScopeInterface::SCOPE_WEBSITES, $helperSetup->getWebsiteCode($websiteId) );*/ $helperSetup->saveConfigValue( 'rw_lorem_ipsum/general/we_said_hello', '0', ScopeInterface::SCOPE_WEBSITES, $websiteId ); } }
删除现有的配置
public function deleteConfiguration($helperSetup) { // Delete value of implicitly default Scope, of implicitly admin store $helperSetup->deleteConfigValue('rw_lorem_ipsum/general/hello_world'); // Delete value of Scope 'store', of store with id 1 $helperSetup->deleteConfigValue( 'rw_lorem_ipsum/general/hello_usa', ScopeInterface::SCOPE_STORE, Store::DISTRO_STORE_ID ); $websiteId = $helperSetup->getWebsiteId('base'); if ($websiteId) { // Delete value of Scope 'website', of website with id $websiteId $helperSetup->deleteConfigValue( 'rw_lorem_ipsum/general/we_said_hello', ScopeInterface::SCOPE_WEBSITES, $websiteId ); } }
创建小部件
public function createNewWidget($helperSetup) { // This is an example function return; $content = <<<EOD <div>Lorem Ipsum</div> EOD; $data = [ 'title' => 'Lorem Ipsum', 'content' => $content, 'is_active' => Block::STATUS_ENABLED ]; $helperSetup->updateCmsBlock('lorem_ipsum', $data, $helperSetup->getStoreId('admin')); $params = [ 'title' => 'A brand new widget', 'sort_order' => '100', 'type_code' => 'cms_static_block', 'theme_path' => 'frontend/RocketWeb/mytheme', 'page_group' => 'virtual_products', 'group_data' => [ 'block' => 'parent_block_name_here', 'for' => 'specific', 'layout_handle' => 'catalog_product_view_type_virtual', // any handler 'entities' => 999999 // Some product id ], 'widget' => [ 'block_id' => 'lorem_ipsum' ] ]; $helperSetup->addWidget( [$helperSetup->getStoreId('admin')], $params ); }
更新事务性电子邮件模板
public function updateEmailTemplate($helperSetup) { // This is an example function return; $template = $helperSetup->getTemplateByCode('Lore Ipsum Forgot Password'); if ($template->getId() <= 0) { return; } $text = <<<EOD {{template config_path="design/email/header_template"}} <p class="greeting">{{trans "%name," name=\$customer.name}}</p> <p>{{trans "There was recently a request to change the password for your account."}}</p> <p>{{trans "If you requested this change, set a new password here:"}}</p> <table class="button" width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <table class="inner-wrapper" border="0" cellspacing="0" cellpadding="0" align="center"> <tr> <td align="center"> <a href="{{var this.getUrl(\$store,'customer/account/createPassword/',[_query:[id:\$customer.id,token:\$customer.rp_token],_nosid:1])}}" target="_blank">{{trans "Set a New Password"}}</a> </td> </tr> </table> </td> </tr> </table> <p>{{trans "If you did not make this request, you can ignore this email and your password will remain the same."}}</p> {{template config_path="design/email/footer_template"}} EOD; $text = $helperSetup->cleanTemplateText($text); $variables = <<<EOD { "var customer.name":"Customer Name", "var this.getUrl(\$store, 'customer/account/createPassword/', [_query:[id:\$customer.id, token:\$customer.rp_token]])":"Reset Password URL" } EOD; $variables = str_replace("\n", '', $variables); $data = [ 'template_text' => $text, 'template_styles' => '', 'template_type' => TemplateTypesInterface::TYPE_HTML, 'template_subject' => '{{trans "Reset your %store_name password" store_name=$store.getFrontendName()}}', 'orig_template_code' => 'customer_password_forgot_email_template', 'orig_template_variables' => $variables, ]; $template->addData($data); $template->save(); }