You are here

function i18n_textfield_process in Internationalization 6

Process callback for textfield elements.

When editing or translating a node, set Javascript to rewrite autocomplete paths to use the node language prefix rather than the current content one.

1 string reference to 'i18n_textfield_process'
i18n_elements in ./i18n.module
Implementation of hook_elements().

File

./i18n.module, line 288
Internationalization (i18n) module.

Code

function i18n_textfield_process($element) {
  global $language;
  static $sent = FALSE;

  // Ensure we send the Javascript only once.
  if (!$sent && isset($element['#autocomplete_path']) && !empty($element['#autocomplete_path']) && variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE) != LANGUAGE_NEGOTIATION_NONE) {

    // Add a JS file for node forms.
    // Determine if we are either editing or translating an existing node.
    // We can't act on regular node creation because we don't have a specified
    // node language.
    $node_edit = $node = menu_get_object() && arg(2) == 'edit' && isset($node->language) && !empty($node->language);
    $node_translate = arg(0) == 'node' && arg(1) == 'add' && !empty($_GET['translation']) && !empty($_GET['language']);
    if ($node_edit || $node_translate) {
      $node_language = $node_edit ? $node->language : $_GET['language'];

      // Only needed if the node language is different from the interface one.
      if ($node_language != $language->language) {
        $languages = language_list();
        if (isset($languages[$node_language])) {
          drupal_add_js(drupal_get_path('module', 'i18n') . '/i18n.js');

          // Pass the interface and content language base paths.
          // Remove any trailing forward slash. Doing so prevents a mismatch
          // that occurs when a language has no prefix and hence gets a path
          // with a trailing forward slash.
          $interface = rtrim(url('', array(
            'absolute' => TRUE,
          )), '/');
          $content = rtrim(url('', array(
            'absolute' => TRUE,
            'language' => $languages[$node_language],
          )), '/');
          $data = array(
            'interface_path' => $interface,
            'content_path' => $content,
          );
          drupal_add_js(array(
            'i18n' => $data,
          ), 'setting');
        }
      }
    }
    $sent = TRUE;
  }
  return $element;
}