variable_get

5 bootstrap.inc variable_get($name, $default)
6 bootstrap.inc variable_get($name, $default)
7 bootstrap.inc variable_get($name, $default = NULL)
8 bootstrap.inc variable_get($name, $default = NULL)

Returns a persistent variable.

Case-sensitivity of the variable_* functions depends on the database collation used. To avoid problems, always use lower case for persistent variable names.

Parameters

$name: The name of the variable to return.

$default: The default value to use if this variable has never been set.

Return value

The value of the variable.

See also

variable_del(), variable_set()

293 calls to variable_get()

File

includes/bootstrap.inc, line 587
Functions that need to be loaded on every Drupal request.

Code

function variable_get($name, $default) {
  global $conf;

  return isset($conf[$name]) ? $conf[$name] : $default;
}

Comments

Using variable_get when #tree = TRUE

Let's take the following example:

How NOT to do it:

function my_module_settings() {
  $form['#tree'] = TRUE; // Prevent flattening the form values
$form['some_fields'] = array(
'#title' => t('Some fields'),
'#type'  => 'fieldset',
'#collapsible' => FALSE,
);
       $form['some_fields'] ['first_field'] = array(
'#title'  => t('First field'),
'#type'  => 'textfield',
                '#default_value' => variable_get('first_field','1st default value')
       );
       $form['some_fields'] ['second_field'] = array(
'#title'  => t('Second field'),
'#type' => 'textfield',
                '#default_value' => variable_get('second_field','2nd default value')
       );
return system_settings_form($form);
}

You might be tempted to do it like that, but if you look in the variables table you will see that there is no variable first_field or second_field, instead we find this variable: some_fields which is a serialized array. This is actually a good way to store multiple variables, because instead of having multiple SQL requests for each variable you have only one.

The solution:

function my_module_settings() {
  $form['#tree'] = TRUE; // Prevent flattening the form values
$form['some_fields'] = array(
'#title' => t('Some fields'),
'#type'  => 'fieldset',
'#collapsible' => FALSE,
);
       // We get the variable some_fields which contains an array with
       // the variables we are looking for (first_field and second_field)
       $some_fields = variable_get('some_fields', array());
       $form['some_fields'] ['first_field'] = array(
'#title'  => t('First field'),
'#type'  => 'textfield',
                '#default_value' => (!empty($some_fields['first_field']))
                    ? $some_fields['first_field']
                    : '1st default value',
       );
       $form['some_fields'] ['second_field'] = array(
'#title'  => t('Second field'),
'#type' => 'textfield',
                '#default_value' => (!empty($some_fields['second_field']))
                    ? $some_fields['second_field']
                    : '2nd default value',
       );
return system_settings_form($form);
}

Hope this helps 'cause it took me a while to figure it out.

thanks! and one more hitch

I just ran into this exact case - wanted to use a fieldset + system_settings_form() + variable_get()s... was just looking at the variables table and thinking maybe I couldn't get the values out one by one - and then saw your post.

I ran into one further issue though. You're using empty() to check whether to use the default value, but what if you want to allow the value to be one that would return true in empty()? (i.e. I want to allow 0 as a value for one field, but 0 is considered an empty value.)

First I considered using isset() on the array returned by variable_get(), but found that even if a field is left empty, its key will still exist in the returned array.

I also wanted to organize the usage of defaults slightly differently than you did. So I ended up merging an array of defaults with the returned array after passing it through a custom filter. Say we have one field expecting a string and a second field expecting a number:

<?php
$some_fields
= array_merge(
  array(
   
'first_field' => 'text default',
   
'second_field' => 3
 
),
 
array_filter( variable_get('some_fields', array()), create_function('$value',
     
'return ( is_numeric($value) && (int) $value === 0) || !empty($value);'
   
)
  )
);
?>

Then I just use the values in $some_fields to set the #default_value of all the relevant fields.

Note: with at least PHP version 5.3.0, you can use anonymous functions rather than create_function (but unfortunately I'm currently working with 5.2.10).

Another Option

Instead of having such a complicated condition, you could use strlen() to determine whether or not the variable is empty. For example:

<?php
array_filter
( variable_get('some_fields', array()), create_function('$value',
     
'return strlen($value);'
   
)
?>

Would return a 0 (false) if $value is empty and a positive number (true) if $value is not empty (including "0"). If you're dead-set on it returning a boolean, you could always cast the return value:

<?php
array_filter
( variable_get('some_fields', array()), create_function('$value',
     
'return (Boolean) strlen($value);'
   
)
?>

Login or register to post comments