prestashop 模块 => 如何从 config.xml 文件获取值
prestashop module => how to reach values from config.xml file
我从 Prestashop 1.7 开始。
我的问题是:config.xml
中的数据如果不能通过Configuration::get('[something in the config.xml file]')
,如何在代码中获取和使用?为什么自定义模块的 __construct()
中未填写的属性(如:$this->name
、$this->tab
、$this->description
、$this->confirmUninstall
)不会自动替换为来自 config.xml
文件的等效值?
这是我试过的:
我一直在看 Creation a PrestaShop 1.7 Module;
我经历过:
- 什么是 PrestaShop 模块?
- 创建第一个模块
- 关于 config.xml 文件
- 添加配置页
我通过使用函数 getContent()
和 displayForm()
体验了如何设置配置。
但我不明白如何从 config.xml
文件中获取数据。
我的 config.xml
文件是这样的(在目录 [PrestashopProject]\module
中):
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>mymodule</name>
<displayName><![CDATA[My module]]></displayName>
<version><![CDATA[1.0]]></version>
<description><![CDATA[Description of my module.]]></description>
<author><![CDATA[Firstname Lastname]]></author>
<tab><![CDATA[front_office_features]]></tab>
<confirmUninstall>Are you sure you want to uninstall?</confirmUninstall>
<is_configurable>1</is_configurable>
<need_instance>0</need_instance>
<limited_countries>fr</limited_countries>
</module>
我的文件 mymodule.php
(也在目录 [PrestashopProject]\module
中)看起来与指南中描述的一样:
class mymodule extends Module {
public function __construct(){
$this->name = 'mymodule';
...
$this->bootstrap = false;
parent::__construct();
$this->displayName = $this->l('My Module');
....
if (!Configuration::get('MYMODULE_NAME')) $this->warning = $this->l('No name provided.');
}
public function install(){
...
return true;
}
public function uninstall(){
...
return true;
}
public function getContent(){
....//stuff to manage the submit of the form designed for configuration settings
}
public function displayForm(){
....//
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
),
'input' => array(
'type' => 'text',
'label' => $this->l('Configuration value'),
'name' => 'confirmUninstall',
'size' => 20,
'required' => true
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
)
);
$helper = new HelperForm();
.... // stuff with $helper like in the doc
$helper->fields_value['confirmUninstall'] = Configuration::get('confirmUninstall');
}
}
我不明白为什么 displayForm()
函数末尾的 Configuration::get('confirmUninstall')
不显示 config.xml
文件中的 <confirmUninstall>Are you sure you want to uninstall?</confirmUninstall>
。
同样在 class mymodule extends Module
的 __construct()
函数中,我可以预期如果我不添加 $this->author = "[author name]";
或 $this->displayName = $this->l('My Module');
它应该从config.xml
文件匹配标签但它不这样做。
按照我从@TheDrot 得到的第一个答案:
我想了解我在这里遗漏了什么。在 Creating a first module 的最底部写着:
During the module’s installation, PrestaShop automatically creates a small config.xml file in the module’s folder, which stores the configuration information. You should be very careful when editing by hand.
的确,起初,我手动创建了 config.xml
文件,因为我没有看到它弹出初始值。
现在我从@TheDrot 得到了答案,我删除了我的 config.xml
文件,从 Prestashop 管理仪表板卸载了我的模块并重新安装了它。
我认为重新安装它会根据 mymodule.php
创建 config.xml
文件。但是没有,我是不是漏设置了?
更新:
它最终创建了 config.xml
文件。我不知道是哪个动作触发了它,但它确实触发了。而自动生成的 config.xml
文件内容匹配 mymodule.php
.
Config.xml 对您没用,因为它是自动生成的,并且如用于优化后端模块列表加载的文档中所述。
这就是您需要在模块构造函数中定义这些选项的原因。
public function __construct() {
...
$this->confirmUninstall = 'Are you sure?';
...
}
并在displayForm()
中使用
$helper->fields_value['confirmUninstall'] = $this->confirmUninstall;
Configuration::get()
与 config.xml 无关,因为它读取数据库中字符串的值 table dbprefix_configuration
.
例如。获取默认商店语言 ID:
$defaultLangID = Configuration::get('PS_LANG_DEFAULT'):
我从 Prestashop 1.7 开始。
我的问题是:config.xml
中的数据如果不能通过Configuration::get('[something in the config.xml file]')
,如何在代码中获取和使用?为什么自定义模块的 __construct()
中未填写的属性(如:$this->name
、$this->tab
、$this->description
、$this->confirmUninstall
)不会自动替换为来自 config.xml
文件的等效值?
这是我试过的:
我一直在看 Creation a PrestaShop 1.7 Module;
我经历过:
- 什么是 PrestaShop 模块?
- 创建第一个模块
- 关于 config.xml 文件
- 添加配置页
我通过使用函数 getContent()
和 displayForm()
体验了如何设置配置。
但我不明白如何从 config.xml
文件中获取数据。
我的 config.xml
文件是这样的(在目录 [PrestashopProject]\module
中):
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>mymodule</name>
<displayName><![CDATA[My module]]></displayName>
<version><![CDATA[1.0]]></version>
<description><![CDATA[Description of my module.]]></description>
<author><![CDATA[Firstname Lastname]]></author>
<tab><![CDATA[front_office_features]]></tab>
<confirmUninstall>Are you sure you want to uninstall?</confirmUninstall>
<is_configurable>1</is_configurable>
<need_instance>0</need_instance>
<limited_countries>fr</limited_countries>
</module>
我的文件 mymodule.php
(也在目录 [PrestashopProject]\module
中)看起来与指南中描述的一样:
class mymodule extends Module {
public function __construct(){
$this->name = 'mymodule';
...
$this->bootstrap = false;
parent::__construct();
$this->displayName = $this->l('My Module');
....
if (!Configuration::get('MYMODULE_NAME')) $this->warning = $this->l('No name provided.');
}
public function install(){
...
return true;
}
public function uninstall(){
...
return true;
}
public function getContent(){
....//stuff to manage the submit of the form designed for configuration settings
}
public function displayForm(){
....//
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
),
'input' => array(
'type' => 'text',
'label' => $this->l('Configuration value'),
'name' => 'confirmUninstall',
'size' => 20,
'required' => true
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
)
);
$helper = new HelperForm();
.... // stuff with $helper like in the doc
$helper->fields_value['confirmUninstall'] = Configuration::get('confirmUninstall');
}
}
我不明白为什么 displayForm()
函数末尾的 Configuration::get('confirmUninstall')
不显示 config.xml
文件中的 <confirmUninstall>Are you sure you want to uninstall?</confirmUninstall>
。
同样在 class mymodule extends Module
的 __construct()
函数中,我可以预期如果我不添加 $this->author = "[author name]";
或 $this->displayName = $this->l('My Module');
它应该从config.xml
文件匹配标签但它不这样做。
按照我从@TheDrot 得到的第一个答案:
我想了解我在这里遗漏了什么。在 Creating a first module 的最底部写着:
During the module’s installation, PrestaShop automatically creates a small config.xml file in the module’s folder, which stores the configuration information. You should be very careful when editing by hand.
的确,起初,我手动创建了 config.xml
文件,因为我没有看到它弹出初始值。
现在我从@TheDrot 得到了答案,我删除了我的 config.xml
文件,从 Prestashop 管理仪表板卸载了我的模块并重新安装了它。
我认为重新安装它会根据 mymodule.php
创建 config.xml
文件。但是没有,我是不是漏设置了?
更新:
它最终创建了 config.xml
文件。我不知道是哪个动作触发了它,但它确实触发了。而自动生成的 config.xml
文件内容匹配 mymodule.php
.
Config.xml 对您没用,因为它是自动生成的,并且如用于优化后端模块列表加载的文档中所述。
这就是您需要在模块构造函数中定义这些选项的原因。
public function __construct() {
...
$this->confirmUninstall = 'Are you sure?';
...
}
并在displayForm()
$helper->fields_value['confirmUninstall'] = $this->confirmUninstall;
Configuration::get()
与 config.xml 无关,因为它读取数据库中字符串的值 table dbprefix_configuration
.
例如。获取默认商店语言 ID:
$defaultLangID = Configuration::get('PS_LANG_DEFAULT'):