entity_translation_handler_field_field.inc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * @file
  4. * This file contains a field handler for entity translation that shows field
  5. * content translated into a specified language.
  6. */
  7. class entity_translation_handler_field_field extends views_handler_field_field {
  8. function option_definition() {
  9. $options = parent::option_definition();
  10. $options['language'] = array(
  11. 'default' => '***CURRENT_LANGUAGE***',
  12. );
  13. return $options;
  14. }
  15. function options_form(&$form, &$form_state) {
  16. parent::options_form($form, $form_state);
  17. $languages = array(
  18. '***CURRENT_LANGUAGE***' => t("Current user's language"),
  19. '***DEFAULT_LANGUAGE***' => t("Default site language"),
  20. LANGUAGE_NONE => t('Language neutral'),
  21. );
  22. $languages = array_merge($languages, locale_language_list());
  23. $form['language'] = array(
  24. '#type' => 'select',
  25. '#title' => t('Language'),
  26. '#options' => $languages,
  27. '#default_value' => $this->options['language'],
  28. '#description' => t('Select the language to display this field in')
  29. );
  30. }
  31. /**
  32. * Overrides parent::field_language, retrieving the language from the handler
  33. * options.
  34. */
  35. function field_language($entity_type, $entity) {
  36. global $language_content;
  37. if (field_is_translatable($entity_type, $this->field_info)) {
  38. $default_language = language_default('language');
  39. $language = str_replace(array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'),
  40. array($language_content->language, $default_language),
  41. $this->options['language']);
  42. // Give the Field Language API a chance to fallback to a different language
  43. // (or LANGUAGE_NONE), in case the field has no data for the selected language.
  44. // field_view_field() does this as well, but since the returned language code
  45. // is used before calling it, the fallback needs to happen explicitly.
  46. $language = field_language($entity_type, $entity, $this->field_info['field_name'], $language);
  47. return $language;
  48. }
  49. else {
  50. return LANGUAGE_NONE;
  51. }
  52. }
  53. }