I can create a variable with "multiple" type depend on some other parameter, for example:


  $variables['node_options_[node_type]'] = array(
    'type' => 'multiple',
    'title' => t('Default options', array(), $options),
    'repeat' => array(
      'type' => 'options',
      'default' => array('status', 'promote'),
      'options callback' => 'node_variable_option_list',
    ),
  );

It's ok, but how to create a custom variable with "multiple" type?
Is it even possible?


I think it's possible and can be very useful for the others. I'm trying to figure out this on my own and this is what I've got so far:


/**
 * Implements hook_variable_info().
 */
function MY_MODULE_variable_info($options) {
  // my custom variable
  $variables['custom_variable'] = array(
    'type' => 'multiple',
    'title' => t('My custom variable', array(), $options),
	'children' => array(
		'first_variable' => array(
			'type' => 'string',
			'index' => 0,
			'name' => 'first_variable',
			'module' => 'MY_MODULE',
			'title' => t('My first variable', array(), $options),
			'default' => 'First value',
			'token' => TRUE,
		),
		'second_variable' => array(
			'type' => 'string',
			'index' => 1,
			'name' => 'second_variable',
			'module' => 'MY_MODULE',
			'title' => t('My second variable', array(), $options),
			'default' => 'Second value',
			'token' => FALSE,
		),
		'third_variable' => array(
			'type' => 'string',
			'index' => 2,
			'name' => 'third_variable',
			'module' => 'MY_MODULE',
			'title' => t('My third variable', array(), $options),
			'default' => 'Third value',
			'token' => FALSE,
		)
	),
    'group' => 'site_information',
  );

  return $variables;
}

Obviously, this is not the way it should be, but as I mentioned in the title - there is no documentation, examples, anything.

Comments

jack-pl’s picture

Status: Needs work » Fixed

Ok, I solve it by my own. Hope it 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 is 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),
  );
}

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

marcvangend’s picture

Thanks for posting, Jacek.