Drupal persistent variables, how the variable set function works
It is customary to use databases or files to store data. But sometimes there is no desire to create a separate table in the database, or it is impractical, and it is not convenient to store data in a file. Then the developer thinks about other ways of storing project information.
Drupal can be used to store persistent variables . It is a fast solution for saving almost any data. First you need to prepare the data, then just call the variable_set function. This function takes two parameters, the first is the name of the variable, the second is its value. The variable name should be in capital letters in English, the value can be any variable, for example, a serialized array.
variable_set ('variable_name', 'variable_value');
When the variable is saved, it can be retrieved later using the variable_get function. It is enough to specify the name of the resulting variable and the default value in the parameters - it will be used if such a variable has not been set.
variable_get ('variable_name', 'default_value');
To remove a previously set variable, you can use a special function - variable_del . It is enough to specify the name of the variable in the parameter.
How does the variable_set function work? It simply stores the value in the Drupal database, in the variable table. This table has two columns - the name of the variable and its data in the form of blob . Before saving, the data is serialized if necessary. It is important to understand that variable_set can interfere with the implementation of multilingualism on the site, since you need to take care of translating data from this table yourself.
What other disadvantages of variable_set are there? You should use this function only for storing small data, and not for any large amounts of information, although this function allows you to do this. It is not recommended to do this, since every time the page is requested, all variables are loaded from the database, even if you did not call variable_get . Imagine how this can slow down the speed of a site if huge values were stored and there are many such variables. The variable_get function just gets ready-made information from the data already loaded earlier.
How to get a list of all variables set by variable_get ? To do this, again, you can refer to the Drupal table - variable . See what values are in your project and what values are there after a fresh installation of Drupal. You can quickly do a fresh install of Drupal or view this table on the Drupal table description site. This will allow you to see the differences and be able to list all the custom variables you have set. Then you can, for example, delete all of them using variable_del .
Thus, we have examined the structure of the variable_set function . It can be useful for storing small data, for storing large volumes it is better not to use it, but to create a separate table in the database.
- IT / Misc 08.07.21 How to make a free translation for a website without an API, translate documents in Google Translate
- IT / Misc 06.07.21 How to make a subscription button on a website, a subscriber base and automatic mailing
- Food / Misc 06.07.21 How to quickly cook delicious fried pies with potatoes and onions
- IT / Misc 04.07.21 Caching - create, load and reset. Where to store the cache, methods and types of caching
- IT / Database 03.07.21 Custom NoSQL - storing data in files and not only in a database. Storing settings, small data and caching files