Module Monday: Variable

Module Monday: Variable

Making Drupal Development Easier, One Variable at a Time

If you’re working on a site with lots of modules, you may find yourself defining your own variables using the Drupal core API function variable_get(). This function takes two arguments, the first being the name of the variable, and the second being the default value for that variable if an administrator has not saved a customized value to the database.
The problem with this is that if you have these variable_get() calls spattered all over your code, and lo and behold a change needs to be made to that default value, you have to track down every time you called variable_get() for that variable and change the default to the new value.
Sure, you could use constants to define the default values in one spot, but even then you may end up needing to rename the constant. What if the API allowed you to specify in just one place what the default value for a variable is, and then everywhere you need the value of that variable, you simply call variable_get(‘myvar’) instead of variable_get(‘myvar’, “and here’s the default value, that heaven forbid ever changes”).

Leave a Reply

Your email address will not be published.