summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.18/sites/all/modules/i18n/i18n_field/i18n_field.module
diff options
context:
space:
mode:
Diffstat (limited to 'kolab.org/www/drupal-7.18/sites/all/modules/i18n/i18n_field/i18n_field.module')
-rw-r--r--kolab.org/www/drupal-7.18/sites/all/modules/i18n/i18n_field/i18n_field.module132
1 files changed, 99 insertions, 33 deletions
diff --git a/kolab.org/www/drupal-7.18/sites/all/modules/i18n/i18n_field/i18n_field.module b/kolab.org/www/drupal-7.18/sites/all/modules/i18n/i18n_field/i18n_field.module
index f39a6a7..b682397 100644
--- a/kolab.org/www/drupal-7.18/sites/all/modules/i18n/i18n_field/i18n_field.module
+++ b/kolab.org/www/drupal-7.18/sites/all/modules/i18n/i18n_field/i18n_field.module
@@ -163,7 +163,6 @@ function i18n_field_field_formatter_view($entity_type, $entity, $field, $instanc
return $element;
}
-
/**
* Implements hook_field_widget_form_alter().
*
@@ -176,8 +175,8 @@ function i18n_field_field_formatter_view($entity_type, $entity, $field, $instanc
function i18n_field_field_widget_form_alter(&$element, &$form_state, $context) {
global $language;
- // Skip the node type edit fields by checking for existing entity
- if (empty($element['#entity'])) {
+ // Don't translate if the widget is being shown on the field edit form.
+ if ($form_state['build_info']['form_id'] == 'field_ui_field_edit_form') {
return;
}
@@ -189,8 +188,38 @@ function i18n_field_field_widget_form_alter(&$element, &$form_state, $context) {
$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 (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'];
+ }
+ 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($element['#language']) || $element['#language'] == LANGUAGE_NONE ? $language->language : $element['#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.
@@ -200,37 +229,60 @@ function i18n_field_field_widget_form_alter(&$element, &$form_state, $context) {
}
// Translate field title if set and it is the default one.
- // When cardinality is 1, $element['value'] is used instead.
if (!empty($instance_current['label']) && $instance_current['label'] != $instance['label']) {
- if (!empty($element['#title']) && $element['#title'] == $instance['label']) {
- $element['#title'] = $instance_current['label'];
- }
- if (isset($element['value']) && !empty($element['value']['#title']) && $element['value']['#title'] == $instance['label']) {
- $element['value']['#title'] = $instance_current['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.
- // When cardinality is 1, $element['value'] is used instead.
if (!empty($instance_current['description']) && $instance_current['description'] != $instance['description']) {
- if (!empty($element['#description']) && $element['#description'] == $instance['description']) {
- $element['#description'] = $instance_current['description'];
- }
- if (isset($element['value']) && !empty($element['value']['#description']) && $element['value']['#description'] == $instance['description']) {
- $element['value']['#description'] = $instance_current['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' => $alter_element['#upload_validators'],
+ );
+ $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' => $alter_element['#upload_validators'],
+ );
+ $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
- if (!empty($element['#options']) && ($translate = i18n_field_type_info($field['type'], 'translate_options')) && !empty($field['settings']['allowed_values'])) {
- $element['#options'] = $translate($field, $i18n_langcode);
- if (isset($element['#properties']) && !empty($element['#properties']['empty_option'])) {
- $label = theme('options_none', array('instance' => $instance, 'option' => $element['#properties']['empty_option']));
- $element['#options'] = array('_none' => $label) + $element['#options'];
- // For some elements, change title to new translated option
- if (!empty($element['#title']) && $field['type'] == 'list_boolean' && !empty($element['#on_value'])) {
- $on_value = $element['#on_value'];
- $element['#title'] = $element['#options'][$on_value];
+ // 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'];
}
}
}
@@ -242,11 +294,15 @@ function i18n_field_field_widget_form_alter(&$element, &$form_state, $context) {
$delta = $context['delta'];
$items = $context['items'];
- // Translate default value if exists and the current value is the default
- if (isset($element['value']['#default_value']) && ($translate = i18n_field_type_info($field['type'], 'translate_default')) &&
- !empty($instance['default_value'][$delta]['value']) && !empty($items[$delta]['value']) &&
- $instance['default_value'][$delta]['value'] === $items[$delta]['value']) {
- $element['value']['#default_value'] = $translate($instance, $items[$delta]['value'], $i18n_langcode);
+ // 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);
+ }
}
}
@@ -344,7 +400,7 @@ function i18n_field_translate_allowed_values($field, $langcode = NULL) {
}
/**
- * Translate field default
+ * Translate field default.
*/
function i18n_field_translate_default($instance, $value, $langcode = NULL) {
return i18n_string_translate(array('field', $instance['field_name'], $instance['bundle'], 'default_value'), $value, array('langcode' => $langcode));
@@ -360,7 +416,17 @@ function i18n_field_translate_property($instance, $property, $langcode = NULL) {
}
/**
- * Get i18n information for fields
+ * Get i18n information for translating fields.
+ *
+ * @param $type
+ * Optional field type.
+ * @param $property
+ * Optional property to get from field type.
+ *
+ * @return
+ * - The property for the field if $type and $property set.
+ * - Array of properties for the field type if only $type is set.
+ * - Array of translation information for all field types.
*/
function i18n_field_type_info($type = NULL, $property = NULL) {
$info = &drupal_static(__FUNCTION__);