2

Im using the Variable module and need to define a variable with the type "multiple" in my custom module. I've been googled whole my day searching for any documentation or just an example and haven't found anything.

There are plenty examples how to set the variable in such type like: string, number, radios, but nothing about the "multiple" type.

My "multiple" variable should look like this:

enter image description here

How can I build this kind of variable using hook_variable_info() ?

2 Answers 2

4

I figure out this on my own. Hope will be useful for someone.

/**
 * Implements hook_variable_info().
 */
function MY_MODULE_variable_info($options) {

    // Use '[custom]' as a placeholder for the variable name. 
    // It will set these properties ('type' and 'multiple') automatically.
    $variables['custom_vars_[custom]'] = array(
        'title'     =>  t('Custom multiple variable'),
        'group'     =>  'site_information',
        'repeat'    =>  array('type' => 'string', 'default' => 'some default value'),
    );

    // if you need to expose your custom variable as token
    // you have to set 'token' key as TRUE, 
    // and ought to set the properties (name, description and type)
    // Notice that 'first' it's a name of variable defined in custom_variable_options().
    $variables['custom_vars_first'] = array(
                    'token' => TRUE,
                    'title' => 'Custom variable',                       // name
                    'description' => 'My first variable description',   // description
                    'type'  => 'string'                                 // type
                    );
  return $variables;
}

/**
 * Implements hook_variable_type_info().
 * @return
 *  An array of information defining variable types. 
 *  The array contains a sub-array for each variable type
 *  with the variable type as the key.
 *  The possible attributes are the same as for hook_variable_info().
 */
function MY_MODULE_variable_type_info() {
    $type['custom'] = array(
        'title' => t('Custom variables'),
        'type'  => 'select',
        'options callback' => 'custom_variable_options',
    );
    return $type;
}

/**
 * Callback for custom options.
 * @return
 *  An array of variables with the variable name as the key.
 */
function custom_variable_options($variable, $options) {
  return array(
    'first'  => t('My first variable', array(), $options), 
    'second' => t('My second variable', array(), $options), 
    'third'  => t('My third variable', array(), $options),
  );
}

Want to see how it works?

  1. Install Variable module
  2. Enable: Variable, Variable store, Variable Admin
  3. Place this code in your custom module (replace 'MY_MODULE' to your module name)
  4. Clear caches.

Results:

admin/config/system/variable -> site information

enter image description here

admin/config/system/variable/group/site_information

enter image description here

admin/config/search/path/patterns

enter image description here

3

You can do that with a regular system_settings_form form, just wrap the elements in a fieldset and set

$form['#tree'] = TRUE;

The variables will then be stored as an array inside a single conf variable.

2
  • Thanks David for your answer, but I need to do that in hook_variable_info() I want to expose some of these variables as tokens. The "multiple" type of variable is already defined as a form element in variable_variable_type_info()
    – Jack-PL
    Commented Jun 14, 2013 at 0:46
  • Is there a way to make the number of variables dynamic like the fields on nodes where the number of values is set to "unlimited"? Commented Jan 3, 2014 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.