tmgmt_field.module 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @file
  4. * Main module file for the Translation Management Source Field module.
  5. */
  6. /**
  7. * Implements hook_tmgmt_source_translation_structure().
  8. *
  9. * This hook is implemented on behalf of the core text module.
  10. */
  11. function text_tmgmt_source_translation_structure($entity_type, $entity, $field, $instance, $langcode, $items) {
  12. $structure = array();
  13. if (!empty($items)) {
  14. $structure['#label'] = check_plain($instance['label']);
  15. foreach ($items as $delta => $value) {
  16. $structure[$delta]['#label'] = t('Delta #@delta', array('@delta' => $delta));
  17. $structure[$delta]['value'] = array(
  18. '#label' => $structure['#label'],
  19. '#text' => $value['value'],
  20. '#translate' => TRUE,
  21. );
  22. // Add format.
  23. $structure[$delta]['format'] = array(
  24. '#label' => '',
  25. '#text' => $value['format'],
  26. '#translate' => FALSE,
  27. );
  28. if ($field['type'] == 'text_with_summary' && !empty($value['summary'])) {
  29. $structure[$delta]['summary'] = array(
  30. '#label' => t('Summary'),
  31. '#text' => $value['summary'],
  32. '#translate' => TRUE,
  33. );
  34. }
  35. }
  36. }
  37. return $structure;
  38. }
  39. /**
  40. * Helper function for retrieving all translatable field values from an entity.
  41. *
  42. * @param $entity_type
  43. * The entity type.
  44. * @param $entity
  45. * An entity object.
  46. * @param $langcode
  47. * The language of retrieved field values.
  48. * @param $only_translatable
  49. * If TRUE, only the fields which are flagged as translatable are returned.
  50. * Defaults to FALSE, which is usually used for node translation, where the
  51. * field translatability does not matter.
  52. *
  53. * @return array
  54. * The structured field data for all translatable fields
  55. */
  56. function tmgmt_field_get_source_data($entity_type, $entity, $langcode, $only_translatable = FALSE) {
  57. try {
  58. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  59. }
  60. catch (Exception $e) {
  61. watchdog_exception('tmgmt field', $e);
  62. return array();
  63. }
  64. $fields = array();
  65. foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
  66. $field = field_info_field($field_name);
  67. $items = field_get_items($entity_type, $entity, $field_name, $langcode);
  68. if ((!$only_translatable || $field['translatable']) && $items) {
  69. if ($data = module_invoke($field['module'], 'tmgmt_source_translation_structure', $entity_type, $entity, $field, $instance, $langcode, $items)) {
  70. $fields[$field_name] = $data;
  71. }
  72. }
  73. }
  74. drupal_alter('tmgmt_field_source_data', $fields, $entity_type, $entity, $langcode);
  75. return $fields;
  76. }
  77. /**
  78. * Populates a field on an object with the provided field values.
  79. *
  80. * @param $entity_type
  81. * The type of $entity.
  82. * @param $entity
  83. * The object to be populated.
  84. * @param $langcode
  85. * The field language.
  86. * @param $data
  87. * An array of values.
  88. * @param $use_field_translation
  89. * TRUE if field translation is being used.
  90. */
  91. function tmgmt_field_populate_entity($entity_type, $entity, $langcode, $data, $use_field_translation = TRUE) {
  92. drupal_alter('tmgmt_field_pre_populate_entity', $data, $entity, $entity_type, $langcode);
  93. foreach (element_children($data) as $field_name) {
  94. if ($field = field_info_field($field_name)) {
  95. $function = $field['module'] . '_field_type_tmgmt_populate_entity';
  96. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  97. $instance = field_info_instance($entity_type, $field_name, $bundle);
  98. if (function_exists($function)) {
  99. $function($entity_type, $entity, $field, $instance, $langcode, $data, $use_field_translation);
  100. }
  101. else {
  102. $field_langcode = $field['translatable'] ? $langcode : LANGUAGE_NONE;
  103. // When not using field translation, make sure we're not storing
  104. // multiple languages.
  105. if (!$use_field_translation) {
  106. $entity->{$field_name} = array($field_langcode => array());
  107. }
  108. foreach (element_children($data[$field_name]) as $delta) {
  109. $columns = array();
  110. foreach (element_children($data[$field_name][$delta]) as $column) {
  111. if (isset($data[$field_name][$delta][$column]['#translation']['#text'])) {
  112. $columns[$column] = $data[$field_name][$delta][$column]['#translation']['#text'];
  113. }
  114. // For elements which are not translatable, keep using the original
  115. // value.
  116. elseif (isset($data[$field_name][$delta][$column]['#translate']) && $data[$field_name][$delta][$column]['#translate'] == FALSE) {
  117. $columns[$column] = $data[$field_name][$delta][$column]['#text'];
  118. }
  119. }
  120. // Make sure the array_merge() gets an array as a first parameter.
  121. if (!isset($entity->{$field_name}[$field_langcode][$delta])) {
  122. $entity->{$field_name}[$field_langcode][$delta] = array();
  123. }
  124. $entity->{$field_name}[$field_langcode][$delta] = array_merge($entity->{$field_name}[$field_langcode][$delta], $columns);
  125. }
  126. }
  127. }
  128. }
  129. drupal_alter('tmgmt_field_post_populate_entity', $entity, $entity_type, $data, $langcode);
  130. }