i18n_field.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @file
  4. * Field and field instance object handlers
  5. */
  6. /**
  7. * Base object for field and field instance
  8. */
  9. class i18n_field_base extends i18n_string_object_wrapper {
  10. /**
  11. * Get base path for object
  12. */
  13. protected function get_base_path() {
  14. $info = entity_get_info($this->object['entity_type']);
  15. if (isset($info['bundles'][$this->object['bundle']]['admin'])) {
  16. $admin = $info['bundles'][$this->object['bundle']]['admin'];
  17. // Extract path information from the bundle.
  18. if (isset($admin['real path'])) {
  19. return $admin['real path'] . '/fields/' . $this->object['field_name'];
  20. }
  21. else {
  22. // We don't have real path, use path instead, may work or not.
  23. return $admin['path'] . '/fields/' . $this->object['field_name'];
  24. }
  25. }
  26. }
  27. }
  28. /**
  29. * Field object
  30. */
  31. class i18n_field extends i18n_field_base {
  32. /**
  33. * Class constructor
  34. *
  35. * For convenience field objects can be built from field info and from field instance.
  36. */
  37. public function __construct($type, $key, $object) {
  38. parent::__construct($type, $key, $object);
  39. // If this is a field instance, get field info but add instance data too.
  40. // This instance data will be used to get the paths to get the edit/translate path.
  41. if (isset($this->object['bundle']) && isset($this->object['entity_type'])) {
  42. $this->object = field_info_field($this->object['field_name']) + array('bundle' => $this->object['bundle'], 'entity_type' => $object['entity_type']);
  43. }
  44. }
  45. /**
  46. * Get edit path for object
  47. */
  48. public function get_edit_path() {
  49. return $this->get_base_path() . '/field-settings';
  50. }
  51. /**
  52. * Get translate path for object
  53. */
  54. public function get_translate_path($langcode = NULL) {
  55. return $this->get_base_path() . '/translate/field' . ($langcode ? '/' . $langcode : '');
  56. }
  57. /**
  58. * Get string context
  59. */
  60. public function get_string_context() {
  61. return array($this->object['field_name'], '#field');
  62. }
  63. /**
  64. * Get translatable properties
  65. */
  66. protected function build_properties() {
  67. $properties = parent::build_properties();
  68. $object = $this->object;
  69. // For select fields field:field_name
  70. if (!empty($object['settings']['allowed_values']) && i18n_field_type_info($object['type'], 'translate_options')) {
  71. //return array('field', $field['field_name'], '#allowed_values');
  72. foreach ($object['settings']['allowed_values'] as $key => $value) {
  73. $properties[$this->get_textgroup()][$object['field_name']]['#allowed_values'][$key] = array(
  74. 'title' => t('Option %name', array('%name' => $value)),
  75. 'string' => $value,
  76. );
  77. }
  78. }
  79. return $properties;
  80. }
  81. /**
  82. * Context to be pre-loaded before translation.
  83. */
  84. protected function get_translate_context($langcode, $options) {
  85. return array(
  86. $this->object['field_name'],
  87. array('#field', '#allowed_values'),
  88. '*'
  89. );
  90. }
  91. /**
  92. * Set field translation for object.
  93. *
  94. * Mot often, this is a direct field set, but sometimes fields may have different formats.
  95. *
  96. * @param $object
  97. * A clone of the object or array. Field instance.
  98. */
  99. protected function translate_field(&$object, $i18nstring, $langcode, $options) {
  100. if ($i18nstring->objectid == '#allowed_values') {
  101. $object['settings']['#allowed_values'][$i18nstring->key] = $i18nstring->format_translation($langcode, $options);
  102. }
  103. else {
  104. parent::translate_field($object, $i18nstring, $langcode, $options);
  105. }
  106. }
  107. }
  108. /**
  109. * Field instance object
  110. */
  111. class i18n_field_instance extends i18n_field_base {
  112. /**
  113. * Get edit path for object
  114. */
  115. public function get_edit_path() {
  116. return $this->get_base_path();
  117. }
  118. /**
  119. * Get translate path for object
  120. */
  121. public function get_translate_path($langcode = NULL) {
  122. return $this->get_base_path() . '/translate' . ($langcode ? '/' . $langcode : '');
  123. }
  124. /**
  125. * Get string context
  126. */
  127. public function get_string_context() {
  128. return array($this->object['field_name'], $this->object['bundle']);
  129. }
  130. /**
  131. * Get translatable properties
  132. */
  133. protected function build_properties() {
  134. $properties = parent::build_properties();
  135. $object = $this->object;
  136. $field = field_info_field($object['field_name']);
  137. // Only for text field types
  138. if (!empty($object['default_value']) && i18n_field_type_info($field['type'], 'translate_default')) {
  139. $format = isset($object['default_value'][0]['format']) ? $object['default_value'][0]['format'] : NULL;
  140. $properties[$this->get_textgroup()][$object['field_name']][$object['bundle']]['default_value']['string'] = $object['default_value'][0]['value'];
  141. $properties[$this->get_textgroup()][$object['field_name']][$object['bundle']]['default_value']['format'] = $format;
  142. }
  143. return $properties;
  144. }
  145. /**
  146. * Set field translation for object.
  147. *
  148. * Mot often, this is a direct field set, but sometimes fields may have different formats.
  149. *
  150. * @param $object
  151. * A clone of the object or array. Field instance.
  152. */
  153. protected function translate_field(&$object, $i18nstring, $langcode, $options) {
  154. if ($i18nstring->property == 'default_value') {
  155. // Render string without applying format
  156. $object['default_value'][0]['value'] = $i18nstring->format_translation($langcode, array('sanitize' => FALSE) + $options);
  157. }
  158. else {
  159. parent::translate_field($object, $i18nstring, $langcode, $options);
  160. }
  161. }
  162. /**
  163. * Context to be pre-loaded before translation.
  164. */
  165. protected function get_translate_context($langcode, $options) {
  166. return array(
  167. $this->object['field_name'],
  168. array('#field', '#allowed_values', $this->object['bundle']),
  169. '*'
  170. );
  171. }
  172. }