prestashop module =>如何从config.xml文件

时间:2017-10-24 21:33:06

标签: prestashop prestashop-1.7

我从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;

我一直在经历:

  1. 什么是PrestaShop模块?
  2. 创建第一个模块
  3. 关于config.xml文件
  4. 添加配置页
  5. 我体验了如何通过使用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');
    
    
        }
    }
    

    我不明白为什么我Configuration::get('confirmUninstall')函数末尾的displayForm()不会显示<confirmUninstall>Are you sure you want to uninstall?</confirmUninstall>文件中的config.xml

    同样在__construct()的{​​{1}}函数中,我可以预期,如果我不添加class mymodule extends Module$this->author = "[author name]";,我应该从{{{}获取数据1}}文件匹配标签,但不会这样做。

    我从@TheDrot得到第一个回答:

    我想了解我在这里缺少的东西。在Creating a first module的最底部写着:

      

    在模块安装期间,PrestaShop会自动在模块的文件夹中创建一个小的config.xml文件,该文件存储配置信息。手动编辑时应该非常小心。

    确实,我首先手动创建了$this->displayName = $this->l('My Module');文件,因为我没有看到它最初弹出。

    现在我从@TheDrot得到了答案,我删除了我的config.xml文件,从Prestashop管理仪表板中卸载了mymodule并重新安装了它。

    我认为重新安装它会根据config.xml创建config.xml文件。但它没有,我错过了一个设置吗?

    更新

    最终创建了config.xml文件。我不知道哪个动作引发了它,但确实如此。自动创建的mymodule.php文件内容与config.xml匹配。

1 个答案:

答案 0 :(得分:1)

Config.xml对您来说是无用的,因为它是自动生成的,并且如在用于优化后端模块列表加载的文档中所述。

这就是您需要在模块构造函数中定义这些选项的原因。

public function __construct() {
    ...
    $this->confirmUninstall = 'Are you sure?';
    ...
}

并在displayForm()

中使用它
$helper->fields_value['confirmUninstall'] = $this->confirmUninstall;

Configuration::get()与config.xml无关,因为它读取数据库表dbprefix_configuration中字符串的值。

EG。获取默认商店语言ID:

$defaultLangID = Configuration::get('PS_LANG_DEFAULT'):