You are here

function i18n_field_field_widget_form_alter in Internationalization 7

Implements hook_field_widget_form_alter().

Translate:

  • Title (label)
  • Description (help)
  • Default value
  • List options

File

i18n_field/i18n_field.module, line 175
Internationalization (i18n) module - Field handling

Code

function i18n_field_field_widget_form_alter(&$element, &$form_state, $context) {
  global $language;

  // Don't translate if the widget is being shown on the field edit form.
  if (isset($form_state['build_info']['form_id']) && $form_state['build_info']['form_id'] == 'field_ui_field_edit_form') {
    return;
  }

  // Skip if we are missing any of the parameters
  if (empty($context['field']) || empty($context['instance']) || empty($context['langcode'])) {
    return;
  }
  $field = $context['field'];
  $instance = $context['instance'];
  $langcode = $context['langcode'];

  // Get the element to alter. Account for inconsistencies in how the element
  // is built for different field types.
  if (isset($element[0]) && count($element) == 1) {

    // Single-value file fields and image fields.
    $alter_element =& $element[0];
  }
  elseif ($field['type'] == 'url' && $field['module'] == 'url' && $field['cardinality'] == 1) {
    $alter_element =& $element;
  }
  elseif (isset($element['value'])) {

    // Number fields. Single-value text fields.
    $alter_element =& $element['value'];
  }
  elseif ($field['type'] == 'entityreference' && isset($element['target_id'])) {

    // Entityreference fields using the entityreference_autocomplete widget.
    $alter_element =& $element['target_id'];
  }
  elseif ($field['type'] == 'node_reference' && isset($element['nid'])) {

    // The node_reference fields using the entityreference_autocomplete widget.
    $alter_element =& $element['nid'];
  }
  else {

    // All other fields.
    $alter_element =& $element;
  }

  // If a subelement has the same title as the parent, translate it instead.
  // Allows fields such as email and commerce_price to be translated.
  foreach (element_get_visible_children($element) as $key) {
    $single_value = $field['cardinality'] == 1;
    $has_title = isset($element['#title']) && isset($element[$key]['#title']);
    if ($single_value && $has_title && $element[$key]['#title'] == $element['#title']) {
      $alter_element =& $element[$key];
      break;
    }
  }

  // The field language may affect some variables (default) but not others (description will be in current page language)
  $i18n_langcode = empty($alter_element['#language']) || $alter_element['#language'] == LANGUAGE_NONE ? $language->language : $alter_element['#language'];

  // Translate instance to current page language and set to form_state
  // so it will be used for validation messages later.
  $instance_current = i18n_string_object_translate('field_instance', $instance);
  if (isset($form_state['field'][$instance['field_name']][$langcode]['instance'])) {
    $form_state['field'][$instance['field_name']][$langcode]['instance'] = $instance_current;
  }

  // Translate field title if set and it is the default one.
  if (!empty($instance_current['label']) && $instance_current['label'] != $instance['label']) {
    if (!empty($alter_element['#title']) && $alter_element['#title'] == check_plain($instance['label'])) {
      $alter_element['#title'] = check_plain($instance_current['label']);
    }
  }

  // Translate field description if set and it is the default one.
  if (!empty($instance_current['description']) && $instance_current['description'] != $instance['description']) {
    if (!empty($alter_element['#description'])) {

      // Allow single-value file fields and image fields to have their
      // descriptions translated. file_field_widget_form() passes the
      // description through theme('file_upload_help'), so i18n_field
      // must do the same.
      $filefield = in_array($field['type'], array(
        'file',
        'image',
      ));
      $single_value = $field['cardinality'] == 1;
      $no_default = empty($alter_element['#default_value']['fid']);
      if ($filefield && $single_value && $no_default) {
        $help_variables = array(
          'description' => field_filter_xss($instance['description']),
          'upload_validators' => isset($alter_element['#upload_validators']) ? $alter_element['#upload_validators'] : array(),
        );
        $original_description = theme('file_upload_help', $help_variables);
        if ($alter_element['#description'] == $original_description) {
          $help_variables = array(
            'description' => field_filter_xss($instance_current['description']),
            'upload_validators' => isset($alter_element['#upload_validators']) ? $alter_element['#upload_validators'] : array(),
          );
          $alter_element['#description'] = theme('file_upload_help', $help_variables);
        }
      }
      elseif ($alter_element['#description'] == field_filter_xss($instance['description'])) {
        $alter_element['#description'] = field_filter_xss($instance_current['description']);
      }
    }
  }

  // Translate list options.
  $has_options = !empty($alter_element['#options']) || $field['type'] == 'list_boolean';
  $has_allowed_values = !empty($field['settings']['allowed_values']);
  $translate = i18n_field_type_info($field['type'], 'translate_options');
  if ($has_options && $has_allowed_values && $translate) {
    $alter_element['#options'] = $translate($field, $i18n_langcode);
    if (isset($alter_element['#properties']) && !empty($alter_element['#properties']['empty_option'])) {
      $label = theme('options_none', array(
        'instance' => $instance,
        'option' => $alter_element['#properties']['empty_option'],
      ));
      $alter_element['#options'] = array(
        '_none' => $label,
      ) + $alter_element['#options'];
    }

    // Translate list_boolean fields using the checkboxes widget.
    if (!empty($alter_element['#title']) && $field['type'] == 'list_boolean' && !empty($alter_element['#on_value'])) {
      $on_value = $alter_element['#on_value'];
      $alter_element['#options'];
      $alter_element['#title'] = $alter_element['#options'][$on_value];

      // For using label instead of "On value".
      if ($instance['widget']['settings']['display_label']) {
        $alter_element['#title'] = $instance_current['label'];
      }
    }
  }

  // Check for more parameters, skip this part if missing.
  if (!isset($context['delta']) || !isset($context['items'])) {
    return;
  }
  $delta = $context['delta'];
  $items = $context['items'];

  // Translate default value.
  $has_default_value = isset($alter_element['#default_value']) && !empty($instance['default_value'][$delta]['value']);
  $storage_has_value = !empty($items[$delta]['value']);
  $translate = i18n_field_type_info($field['type'], 'translate_default');
  if ($has_default_value && $storage_has_value && $translate) {

    // Compare the default value with the value currently in storage.
    if ($instance['default_value'][$delta]['value'] === $items[$delta]['value']) {
      $alter_element['#default_value'] = $translate($instance, $items[$delta]['value'], $i18n_langcode);
    }
  }
}