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

Earnie Boyd earnie at users.sourceforge.net
Thu May 3 19:46:46 UTC 2007

Quoting Gabor Hojtsy <gabor at hojtsy.hu>:

> Earnie Boyd wrote:
>>> **We need a central place to define variable defaults**
>> At the top of the module:
>> <?php
>> define('MY_DEFAULT_VALUE', t('my_default_value'));
>> ?>
>> Then in the code:
>> <?php
>> variable_get ('my_find_var', MY_DEFAULT_VALUE);
>> ?>
>> I then only have one place to edit.
>> The central place is at the top of the module with a define statement.
>>>   hook_settings() {
>>>     return array(
>>>       'my_fine_var' => 'my_default_value',
>>>     );
>>>   }
>> 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.
> Ernie, I am fine with this solution too for Drupal itself.
> BUT locale module will require a list of variables and their default 
> values defined somewhere, so we will need to do that ourselfs in this 
> case. It seemed to be odd to do this as part of the hook_locale() 
> introduced today, but if hook_settings() seems to be 
> overcomplication, then we need to go that way. I thought that if
>  - we can save extra typing and value passing on variable_get calls
>  - we have a hook to reuse when we need a list of variables
>    to present for localization
> adds pluses over just using constants.
> Sure, only using constants for default variable values could be make 
> variable usage better, but I think a hook do more just by being a 
> central place and not a set of unrelated constants.

What if you used get_defined_constants() to return an associative array 
of constant names?  You now have a key to translate the default with.  
We could go further and add some namespace rules to the naming of 
constants like DRUPAL__ for Drupal core constants and MY_MODULE__ for 
module specific namespace.  How does this sound?


More information about the development mailing list