...
But having a getContent()
public method in the MyModule
object does only make the "Configure" link appear; it does not create the configuration page out of nowhere. We are going to explain how to create one, where we will be able to edit the content of the MYMODULE_NAME
variable that we stored in the ps_configuration
data table.
...
The getContent() method
Tout d'abord, voici le code complet pour notre méthode First, here is the complete code for the getContent()
method:
Code Block |
---|
public function getContent() { $output = null; if (Tools::isSubmit('submit'.$this->name)) { $my_module_name = strval(Tools::getValue('MYMODULE_NAME')); if (!$my_module_name || empty($my_module_name) || !Validate::isGenericName($my_module_name)) $output .= $this->displayError( $this->l('Invalid Configuration value') ); else { Configuration::updateValue('MYMODULE_NAME', $my_module_name); $output .= $this->displayConfirmation($this->l('Settings updated')); } } return $output.$this->displayForm(); } |
La méthode The getContent()
method is the first one to be called when the configuration page is loaded. Therefore, we use it to first update any value that might have been submitted by the form that the configuration page contains.
Here is a line by line explanation:
Tools::isSubmit()
est une méthode propre à PrestaShopis is a PrestaShop-specific method, which checks if the indicated form has been validated.
In this case, if the configuration form has not yet been validated, the wholeif()
block is skipped and PrestaShop will only use the last line, which displays the configuration with the current values, as generated by thedisplayForm()
method.Tools:getValue()
is a PrestaShop-specific method, which retrieve the content of thePOST
orGET
array in order to get the value of the specified variable.
In this case, we retrieve the value of theMYMODULE_NAME
form variable, turn its value into a text string using thestrval()
method, and stores it in the$my_module_name
PHP variable.- We then check for the existence of actual content in
$my_module_name
, including the use ofValidate::isGenericName()
.
TheValidate
object contains many data validation methods, among which isisGenericName()
, a method that helps you keep only strings that are valid PrestaShop names – meaning, a string that does not contain special characters, for short. - If any of these checks fail, the configuration will open with an error message, indicating that the form validation failed.
The$output
variable, which contains the final rendition of the HTML code that makes the configuration page, thus begins with an error message, created using PrestaShop'sdisplayError()
method. This method returns the correct HTML code for our need, and since that code is first in$output
, this means the configuration will open with that message. - If all these checks are successful, this means we can store the value in our database.
As we saw earlier in this tutorial, theConfiguration
object has just the method we need:updateValue()
will store the new value forMYMODULE_NAME
in the configuration data table.
To that, we add a friendly message to the user, indicating that the value has indeed been saved: we use PrestaShop'sdisplayConfirmation()
method to add that message as the first data in the$output
variable – and therefore, at the top of the page. - Finally, we use the custom
displayForm()
method (which we are going to create and explain in the next section) in order to add content to$output
(whether the form was submitted or not), and return that content to the page.
Note that we could have included the code fordisplayForm()
right withingetContent()
, but chose to separate the two for readability and separation of concerns.
...