You are here

function i18n_node_textfield_process in Internationalization 7

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.

@todo Update D7

File

i18n_node/i18n_node.module, line 360
Internationalization (i18n) module - Node type handling

Code

function i18n_node_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_DEFAULT) != LANGUAGE_NEGOTIATION_DEFAULT) {

    // 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_node') . '/i18n_node.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;
}