Multilingual configuration forms in Drupal 7

Article originally published at Medium

Some time ago I had to implement a configuration form in Drupal to save multilingual variables and after some research I found that the best way to do it is using the variable module alongside i18n_variable (part of i18n).

First thing you need to do is to define a variable group; to do this, you should implement hook_variable_group_info like this:

 * Implements hook_variable_group_info().
function example_variable_group_info() {
  $groups['example_group'] = array(
    'title' => t('Example Group'),
    'description' => t('Example group variables'),
    'access' => 'administer site configuration',
    'path' => 'admin/config/example',
  );  return $groups;

Once done that; you need to declare the variables that belong to that group; for our example we'll declare two variables. In order to do that; you should implement hook_variable_info like this:

 * Implements hook_variable_info().
function example_variable_info($options) {
  $variables['example_var1'] = array(
    'type' => 'string',
    'title' => t('Example Var 1'),
    'description' => t('Example Var 1', array(), $options),
    'default' => t('Example Var 1 default text', array(), $options),
    'localize' => TRUE,
    'group' => 'example_group',
  );$variables['example_var2'] = array(
    'type' => 'text_format',
    'title' => t('Example Var 2'),
    'description' => t('Example Var 2', array(), $options),
    'default' => t('Example Var 2 default text', array(), $options),
    'localize' => TRUE,
    'group' => 'example_group',
  );  return $variables;

After that; you should define a menu item for the path where you're going to show the configuration form.

 * Implements hook_menu().
function example_menu() {
  $items['admin/config/example'] = array(
    'title' => 'Example Config',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('variable_group_form', 'example_group'),
    'access arguments' => array('administer site configuration'),
    'type' => MENU_NORMAL_ITEM,
  );  return $items;

Now; if you go to that path (admin/config/example); you should see the variables; however they're still not multilingual; you should enable multilingual on those variables. To do that; go to admin/config/regional/i18n/variable and check all your variables to make them multilingual. Now; when you go to admin/config/example you'll have the possibility to edit the value for your variables in all the enabled languages in your site.

I hope it helps.

Hide Image
Submitted by kporras07 on Thu, 01/05/2017 - 03:31