Any comments regarding default variable namespaces?
Right now I have a lot of long variable names just to avoid clashes with my own stuff.
variable_get('my_module_name_my_variable_name_var', MY_DEFAULT_VALUE);
I was wondering if we should have variables within their own module namespaces.
Sort of like local module variables (stored in the db of course) that would be guarrenteed
unique???
Also, a side benefit of namespaces might be something like this:
$vars = variable_get_array('my_module_name');
which could load all the variables for a module with one db call.
Quoting Gabor Hojtsy <gabor@hojtsy.hu>:
> Hi,
>
> To be able to translate variables to multiple languages in Drupal
> nicely, we would need a small conceptual change, which would benefit
> all of the Drupal developers, so I am posting the call here in hopes
> we have someone or a small group to pick this task up.
>
> **We need a central place to define variable defaults**
>
At the top of the module:
<?php
define('MY_DEFAULT_VALUE', t('my_default_value'));
?>
> Simple! Now whenever you need a variable, you do
>
> variable_get('my_fine_var', 'my_default_value');
>
Then in the code:
<?php
variable_get ('my_find_var', MY_DEFAULT_VALUE);
?>
> The problem with this is that you need to repeat this multiple times,
> and of course there is a chance you need to modify it later on, so
> you need to find all places a variable is used. Not good. Remember
> the changes from bluemarine to garland, we have been fixing
> theme_default errors for days, finding out places where the variable
> was used...
>
I then only have one place to edit.
> So we need a central place to define variable defaults. For Drupal 6
> this is enough now:
>
The central place is at the top of the module with a define statement.
> hook_settings() {
> return array(
> 'my_fine_var' => 'my_default_value',
> );
> }
>
> Have fun with naming it hook_settings(), as far as I see, it is an
> appropriate name, and not taken at the moment :) So Drupal can do a
> module_invoke_all() on hook_settings() and collect defaults to all
> variables from the modules defining them. It gets easier and shorter
> to use variables:
>
> variable_get('my_fine_var');
>
variable_get('my_fine_var', MY_DEFAULT_VALUE) is simple enough. I
don't understand why we would need to add this complication and
overhead.
Earnie