The Configuration storage
The greeting text is currently written right in the code, and the shop owner cannot change it. Module settings are kept by the Configuration class — key-value pairs in the ps_configuration database table. The core itself uses the same storage.
Writing and reading
Configuration::updateValue('ACMEHELLO_GREETING', 'Welcome!');
$greeting = Configuration::get('ACMEHELLO_GREETING');updateValue() creates a value or replaces the existing one. get() returns it as a string, or false if the key does not exist.
A key is written in capital letters and starts with the module name — this way it does not clash with someone else's. The key length is up to 254 characters.
A default value on install
A setting is created in install() and removed in uninstall():
// modules/acmehello/acmehello.php
public function install(): bool
{
return parent::install()
&& $this->registerHook('displayHome')
&& $this->registerHook('displayFooterProduct')
&& $this->registerHook('actionValidateOrder')
&& $this->registerHook('actionFrontControllerSetMedia')
&& Configuration::updateValue('ACMEHELLO_GREETING', 'Welcome!');
}
public function uninstall(): bool
{
return Configuration::deleteByName('ACMEHELLO_GREETING')
&& parent::uninstall();
}After the module is removed, none of its rows stay in the database.
Using it in a hook
// modules/acmehello/acmehello.php
public function hookDisplayHome(array $params): string
{
$this->context->smarty->assign([
'greeting' => Configuration::get('ACMEHELLO_GREETING'),
]);
return $this->fetch('module:acmehello/views/templates/hook/home.tpl');
}Reset the module so that install() writes the value:
php bin/console prestashop:module reset acmehelloThe home page looks the same, but the text now comes from the database. You can check it with a query:
SELECT name, value FROM ps_configuration WHERE name LIKE 'ACMEHELLO%';The query returns one row with the key ACMEHELLO_GREETING and the greeting text.
Methods
| Method | What it does |
|---|---|
Configuration::get($key) | Reads a value |
Configuration::updateValue($key, $value) | Writes |
Configuration::deleteByName($key) | Deletes |
Configuration::hasKey($key) | Checks whether the key exists |
Configuration::getMultiple([$key1, $key2]) | Reads several keys into an array |
Value types
The storage holds strings. A number or a flag is cast to the right type when read:
Configuration::updateValue('ACMEHELLO_LIMIT', 5);
Configuration::updateValue('ACMEHELLO_ENABLED', 1);
$limit = (int) Configuration::get('ACMEHELLO_LIMIT');
$enabled = (bool) Configuration::get('ACMEHELLO_ENABLED');An array is saved as a JSON string: json_encode() on write and json_decode() on read.
Values in several languages
If the text must differ by language, pass an array whose key is the language id:
$values = [];
foreach (Language::getLanguages(false) as $language) {
$values[$language['id_lang']] = 'Welcome!';
}
Configuration::updateValue('ACMEHELLO_GREETING', $values);
$greeting = Configuration::get('ACMEHELLO_GREETING', $this->context->language->id);The second argument of get() is the id of the language the value is needed for.
Core settings
The settings of the shop itself are read the same way.
| Key | What is in it |
|---|---|
PS_SHOP_NAME | The shop name |
PS_SHOP_EMAIL | The shop email |
PS_LANG_DEFAULT | The default language id |
PS_CURRENCY_DEFAULT | The default currency id |
Common mistake
By default Configuration::updateValue() strips HTML from the value. You save text with tags and read it back without them. For HTML pass the third argument: Configuration::updateValue('ACMEHELLO_TEXT', $html, true) — and check the value with Validate::isCleanHtml() first, so that no script gets into the settings.