[development] low hanging fruit for Drupal 6: variable defaults

Earnie Boyd earnie at users.sourceforge.net
Thu May 3 12:39:52 UTC 2007


Quoting Gabor Hojtsy <gabor at 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


More information about the development mailing list