t('Fields'), 'description' => t('Configurable fields descriptions, defaults, options, etc.'), 'format' => FALSE, // This group doesn't have formatted strings ); return $groups; } /** * Implements hook_i18n_string_list() */ function i18n_field_i18n_string_list($group) { if ($group == 'field' || $group == 'all') { } } /** * Implements hook_field_attach_form(). * * After the form fields are built */ function i18n_field_field_attach_form($entity_type, $entity, $form, $form_state, $langcode) { } /** * Implements hook_field_info_alter(). * * Cached, invoked only after field info is rebuilt */ function i18n_field_field_info_alter(&$info) { } /** * Implements hook_field_formatter_info(). */ function i18n_field_field_formatter_info() { $types = array(); foreach (i18n_field_type_info() as $type => $info) { if (!empty($info['translate_options'])) { $types[] = $type; } } return array( 'i18n_list_default' => array( 'label' => t('Default translated'), 'field types' => $types, ), ); } /** * Implements hook_field_formatter_view(). */ function i18n_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { $element = array(); switch ($display['type']) { case 'i18n_list_default': if (($translate = i18n_field_type_info($field['type'], 'translate_options'))) { $allowed_values = $translate($field, $langcode); } else { // Defaults to list_default behavior $allowed_values = list_allowed_values($field); } foreach ($items as $delta => $item) { if (isset($allowed_values[$item['value']])) { $output = field_filter_xss($allowed_values[$item['value']]); } else { // If no match was found in allowed values, fall back to the key. $output = field_filter_xss($item['value']); } $element[$delta] = array('#markup' => $output); } break; } return $element; } /** * Implements hook_field_widget_info_alter() * * Cached, invoked only after widget info is rebuilt */ function i18n_field_field_widget_info_alter(&$info) { } /** * Implements hook_field_widget_properties_alter(). * * This is called for the entity edit form and for the fields edit form */ function i18n_field_field_widget_properties_alter(&$widget, $context) { // Skip the node type edit fields by checking for existing entity if (!empty($context['entity']) && !empty($widget['module']) && function_exists($function = $widget['module'] . '_field_widget_form')) { $widget['module'] = 'i18n_field'; $widget['i18n_field_callbacks'][] = $function; } } function i18n_field_field_extra_fields_display_alter(&$displays, $context) { } /** * Implements hook_field_display_alter(). * * Called only when refreshing cache */ function i18n_field_field_display_alter(&$display, $context) { } function i18n_field_field_storage_info_alter(&$info) { } /** * Field API callback to rewrite field element * * Translate: * - Description (help) * - Default value * @see field_default_form() */ function i18n_field_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element) { global $language; // The field language may affect some variables (default) but not others (description will be in current page language) $langcode = empty($element['#language']) || $element['#language'] == LANGUAGE_NONE ? $language->language : $element['#language']; $delta = $element['#delta']; $translate = array( 'description' => array('filter' => 'field_filter_xss'), //'default_value' => array('langcode' => $langcode), ); foreach ($translate as $property => $options) { } // Translate field description if set if (!empty($instance['description'])) { $element['#description'] = i18n_field_translate_property($instance, 'description'); } // Translate default value if exists and the current value is the default if (($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']) { $items[$delta]['value'] = $translate($instance, $items[$delta]['value'], $langcode); } // Translate list options if (($translate = i18n_field_type_info($field['type'], 'translate_options')) && !empty($field['settings']['allowed_values'])) { $field['settings']['allowed_values'] = $translate($field, $langcode); } // Redirect through original module_field_widget_form() if (!empty($instance['widget']['i18n_field_callbacks'])) { foreach ($instance['widget']['i18n_field_callbacks'] as $function) { $element = $function($form, $form_state, $field, $instance, $langcode, $items, $delta, $element); } } return $element; } /** * Implements hook_field_create_field() */ function i18n_field_field_create_field($field) { i18n_field_field_update_strings($field); } /** * Implements hook_field_create_instance(); */ function i18n_field_field_create_instance($instance) { i18n_field_instance_update_strings($instance); } /** * Implements hook_field_delete_instance(); */ function i18n_field_field_delete_instance($instance) { foreach (array('description', 'default_value') as $property) { i18n_string_remove(array('field', $instance['bundle'], $instance['field_name'], $property)); } } /** * Implements hook_field_update_instance(); */ function i18n_field_field_update_instance($instance, $prior_instance) { i18n_field_instance_update_strings($instance); } /** * Implements hook_field_update_field() */ function i18n_field_field_update_field($field) { i18n_field_field_update_strings($field); } /** * Update field strings */ function i18n_field_field_update_strings($field) { // For select fields field:field_name if (!empty($field['settings']['allowed_values']) && i18n_field_type_info($field['type'], 'translate_options')) { i18n_string_update(array('field', $field['field_name'],'#allowed_values'), $field['settings']['allowed_values']); } } /** * Update field instance strings */ function i18n_field_instance_update_strings($instance) { $field = field_info_field($instance['field_name']); if (isset($instance['description'])) { i18n_string_update(array('field', $instance['field_name'], $instance['bundle'], 'description'), $instance['description']); } // Only for text field types if (isset($instance['default_value']) && i18n_field_type_info($field['type'], 'translate_default')) { $format = isset($instance['default_value'][0]['format']) ? $instance['default_value'][0]['format'] : NULL; i18n_string_update(array('field', $instance['field_name'], $instance['bundle'], 'default_value'), $instance['default_value'][0]['value'], array('format' => $format)); } } /** * Returns the array of translated allowed values for a list field. * * The strings are not safe for output. Keys and values of the array should be * sanitized through field_filter_xss() before being displayed. * * @param $field * The field definition. * * @return * The array of allowed values. Keys of the array are the raw stored values * (number or text), values of the array are the display labels. */ function i18n_field_translate_allowed_values($field, $langcode = NULL) { if (!empty($field['settings']['allowed_values'])) { return i18n_string_translate(array('field', $field['field_name'], '#allowed_values'), $field['settings']['allowed_values'], array('langcode' => $langcode)); } else { return array(); } } /** * 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)); } /** * Translate field property */ function i18n_field_translate_property($instance, $property, $langcode = NULL) { return i18n_string_translate(array('field', $instance['field_name'], $instance['bundle'], $property), $instance[$property], array('langcode' => $langcode)); } /** * Get i18n information for fields */ function i18n_field_type_info($type = NULL, $property = NULL) { $info = &drupal_static(__FUNCTION__); if (!isset($info)) { $info = module_invoke_all('i18n_field_info'); drupal_alter('i18n_field_info', $info); } if ($property) { return isset($info[$type]) && isset($info[$type][$property]) ? $info[$type][$property] : NULL; } elseif ($type) { return isset($info[$type]) ? $info[$type] : array(); } else { return $info; } } /** * Implements hook_i18n_field_info() */ function i18n_field_i18n_field_info() { $info['text'] = $info['text_long'] = $info['text_with_summary'] = array( 'translate_default' => 'i18n_field_translate_default', ); $info['list_text'] = $info['list_boolean'] = array( 'translate_options' => 'i18n_field_translate_allowed_values', ); return $info; }