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

Larry Garfield larry at garfieldtech.com
Fri May 4 14:26:44 UTC 2007


On Friday 04 May 2007, Dries Buytaert wrote:

> Anyway, let's step back for a bit. It seems like we're trying to
> solve two problems in this thread:
>
>   1. For convenience, we want a central place to define a variable's
> default value.
>
>   2. Out of necessity, we want a mechanism to let the locale system
> translate variables that have dynamic content shown on the UIs (i.e.
> the footer message).
>
> 1 and 2 are somewhat related -- if we had a mechanism to introspect
> what variables are available, we might be able to extract their
> default value from a central place, and the locale system could
> figure out what variables need to be translated.
>
> So, each variable should have two properties: (i) a default value and
> (2) a "dynamic translation"-flag (TRUE if translatable, FALSE if not)
> and we should be able to poll both.  Gabor's original question was:
> what would be the best implementation?

Because the variables could change at runtime, I think a hook_variables() (or 
similar) would be the best approach.  Core may not use more than just node 
type, but I know some contribs do, like form IDs.

hook_variables() {
  $items = array();
  $items['mymod_num_things'] = array(
    '#default_value' => 2,  // Same syntax as FAPI uses
    '#realm' => 'mymod', // optional
    '#translatable' => FALSE, // probably default false
    '#cacheable' => TRUE, // default to TRUE if under X chars when serialized
    '#serialize' => FALSE, // default to true for object/array, else false 
  );
  foreach (node_get_types('name') as $type) {
    $items['mymod_things_for_' . $type] = array(
      '#default_value' => array(),  
    );
  }

  return $items;
}

I kinda like the idea of using FAPI syntax here, as then 
system_settings_form() can fill in defaults for us if they're not specified.  
(I suppose it could anyway, but meh. <g>)  I leave that to Gabor to decide.

I'm undecided on whether non-cached big variables should still be considered 
variables or should have some other namespace/table.  I guess that depends on 
how expensive it would be to make variable_get() handle both cases.

-- 
Larry Garfield			AIM: LOLG42
larry at garfieldtech.com		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson


More information about the development mailing list