Download & Extend

Allow forms to write i18n domain variables

Project:Domain Internationalization
Version:6.x-1.x-dev
Component:User interface
Category:feature request
Priority:normal
Assigned:Unassigned
Status:postponed

Issue Summary

Hello,

I like to overwrite i18n domain variables in the normal system form, so user dont have to use the domains -> Multilingual variables. It should work like normal i18n variables. I created the following code, which do that. It don't touch any other functions. Use it when you like:

Code in domain_i18n.module

/**
* preprocess save via Form
*/
function domain_i18n_form_alter(&$form, $form_state, $form_id) {
  global $language;
  global $_domain;
  $domain_id = $_domain['domain_id'];
  $variables = variable_get('domain_i18n_variables', false);
  $domain_variable_settings = domain_i18n_variable_settings($domain_id);
  if (isset($form['#theme']) && $form['#theme'] == 'system_settings_form' && $domain_variable_settings && $variables) {
     if ($domain_i18n_variables = domain_i18n_form_alter_variables_settings($form, $domain_variable_settings)) {
          array_unshift($form['#submit'], 'domain_i18n_form_alter_variables_submit');
          $form['#domain_i18n_variables'] = $domain_i18n_variables;
          $form['buttons']['submit']['#value'] = $form['buttons']['submit']['#value'].' d-l';
     }

  }

}

/**
* Check for variables in form and add description/class.
*/
function domain_i18n_form_alter_variables_settings(&$form, &$variables) {
  $result = array();
  foreach (element_children($form) as $field) {
    if (count(element_children($form[$field])) && empty($form[$field]['#tree'])) {
      $result += domain_i18n_form_alter_variables_settings($form[$field], $variables);
    }
    elseif (in_array($field, $variables)) {
      $form[$field]['#attributes']['class'] = !empty($form[$field]['#attributes']['class']) ? $form[$field]['#attributes']['class'] . ' domain-i18n-variable' : 'domain-i18n-variable';
      $form[$field]['#description'] = !empty($form[$field]['#description']) ? $form[$field]['#description'] : '';
      $form[$field]['#description'] .= ' <strong>'. t('This is a multilingual-domain variable.') .'</strong>';
      $result[$field] = !empty($form[$field]['#title']) ? $form[$field]['#title'] : $field;
    }
  }
  return $result;
}

/**
* Save variables and remove them from domain-i18n-form.
*/
function domain_i18n_form_alter_variables_submit($form, &$form_state) {
  global $_domain;
  $domain_id = $_domain['domain_id'];
  $domain_variable_settings = domain_i18n_variable_settings($domain_id);
  $domain_i18n_variables = variable_get('domain_i18n_variables', array());
  $langcode = i18n_get_lang();
  foreach ($form_state['values'] as $key => $value) {
    if (in_array($key, $domain_variable_settings)) {
      if ($op == t('Reset to defaults')){
          domain_i18n_variable_delete($domain_id, $key, $langcode);
      }
      else {
        if (is_array($value) && isset($form_state['values']['array_filter'])) {
          $value = array_keys(array_filter($value));
        }
         $domain_variable_values = array();
         $domain_variable_values[] = array(
            'name' => $key,
            'domain' => $domain_id,
            'language' => $langcode,
            'value' => serialize( $value),
         );
         domain_i18n_variable_delete($domain_id, $key, $langcode);
         domain_i18n_variable_insert($domain_variable_values);
         //drupal_set_message(("Updated domain specific multilingual variables:".print_r($domain_variable_values,true)));
      }
      unset($form_state['values'][$key]);
     // drupal_set_message(("unset domain specific multilingual variables:".$key));
    }

  }
//  drupal_set_message(t('Updated domain specific multilingual variables.'));
}

Comments

#1

Status:needs review» postponed

To be honest I opted to have these in a separate form for a reason - only a small set of users will be using Domain i18n alone, most sites use combination of i18n and domain i18n to translate user defined strings. If all of these translations of the string are managed from the same form (which is confusing for i18n alone) I could never keep track of the support tickets. Also your approach will work for a smaller DA site with only a few domains while switching domains and languages for 100+ domains is not really an option any more.

So I'm marking this as postponed for now - I might revisit this as configuration option (eg. you can choose if you prefer to use one form to alter it for all or switch your specific form per domain and save it).

#2

On the site i used it, i need to have a login for a single-domain manager. So they dont have to go over the domain-form and can edit the variable like in a single domain. I also use both, Domain i18n and i18n. And with the code i posted i'm able to use both, the domain-form with all variables and the form in the module.

However, a configuration option could be nice.