Drupal Droppings: variable_get tamed!
In Drupal there is the function called variable_get() which is the
means by which a simple persistence mechanism has been provided for
modules. The name that you give to your variable should always be
prefixed (under normal circumstances) with the name of the module. So
for example, saving a variable for module ‘foo’ would probably look
like this :-
variable_set('foo_value',42);
If you are a good little coder and you are writing a nice tidy little
back-end administration page to support some module features then you
ought to be always supplying your calls to variable_get() with a
suitable default value because it is possible that a module can be
called upon to do its job without the user ever visting the back end
administration pages.
One thing that you can do is to use variable_set() to set defaults for
everything in the module.install file, that way every variable that
your module uses will have a sensible value set for it by the time the
real code is called upon to do its thing. If you haven’t done that
then the only other way to ensure that you always get a sensible value
is to always supply a default value to the variable_get() call.
Sometimes this can get a little tedious especially if, like me, you
also always use named constants to refer to things to avoid repeating
yourself in the code. This is a good practice but sometimes typing
long names can be a real pain. Yeah, I know, editors can help but
wouldn’t it be nice if there was a cleaner way to always call
variable_get() with a property name and have it always return either
the current value or the default value and at the same time minimise
the typing and thus opportunity for error ?
Here it is!
Step one. Make your variable name the same as the constant name.
Step two. Define this function somewhere in your module.
function _vg($k) { $v = defined(strtoupper($k)) ? constant(strtoupper($k)) : NULL; return variable_get( $k, $v); }
Note that we have to use the strtoupper() function on the constant name in order to conform to the convention that all constants are defined in upper case. This is an interesting ‘quirk’ in PHP I guess because by default the constants are case sensitive. However, if you stick the the rule of *always* defining your constants in upper case then this function is for you!
All you have to do now is make sure you define your constants and then
you can just use _vg() with the name of the constant… if the
constant is defined then it will use the current value of the constant
as the default value, otherwise it will use whatever value is
currently set for that constant.
This means that instead of typing this all the time :-
define('FOO_HIT_COUNTER_VALUE',42); // longhand $thing = variable_get('foo_hit_counter_value', FOO_HIT_COUNTER_VALUE); // shorter $thing = _vg('foo_hit_counter_value');
Having used this pattern for a while I have noticed that I make less
mistakes and that it becomes easier on the brain to sit down and
actually declare a shed load of constants in the .module file and
coding becomes more disciplined.
Enjoy.