View source
<?php
include_once 'tmgmt_field.field_collection.inc';
function text_tmgmt_source_translation_structure($entity_type, $entity, $field, $instance, $langcode, $items) {
$structure = array();
if (!empty($items)) {
$structure['#label'] = check_plain($instance['label']);
foreach ($items as $delta => $value) {
$structure[$delta]['#label'] = t('Delta #@delta', array(
'@delta' => $delta,
));
$structure[$delta]['value'] = array(
'#label' => $structure['#label'],
'#text' => $value['value'],
'#translate' => TRUE,
);
$structure[$delta]['format'] = array(
'#label' => '',
'#text' => $value['format'],
'#translate' => FALSE,
);
if ($field['type'] == 'text_with_summary' && !empty($value['summary'])) {
$structure[$delta]['summary'] = array(
'#label' => t('Summary'),
'#text' => $value['summary'],
'#translate' => TRUE,
);
}
}
}
return $structure;
}
function tmgmt_field_get_source_data($entity_type, $entity, $langcode, $only_translatable = FALSE) {
try {
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
} catch (Exception $e) {
watchdog_exception('tmgmt field', $e);
return array();
}
$fields = array();
foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
$field = field_info_field($field_name);
$items = field_get_items($entity_type, $entity, $field_name, $langcode);
if ((!$only_translatable || $field['translatable']) && $items) {
if ($data = module_invoke($field['module'], 'tmgmt_source_translation_structure', $entity_type, $entity, $field, $instance, $langcode, $items)) {
$fields[$field_name] = $data;
}
}
}
drupal_alter('tmgmt_field_source_data', $fields, $entity_type, $entity, $langcode);
return $fields;
}
function tmgmt_field_populate_entity($entity_type, $entity, $langcode, $data, $use_field_translation = TRUE) {
drupal_alter('tmgmt_field_pre_populate_entity', $data, $entity, $entity_type, $langcode);
foreach (element_children($data) as $field_name) {
if ($field = field_info_field($field_name)) {
$function = $field['module'] . '_field_type_tmgmt_populate_entity';
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
$instance = field_info_instance($entity_type, $field_name, $bundle);
if (function_exists($function)) {
$function($entity_type, $entity, $field, $instance, $langcode, $data, $use_field_translation);
}
else {
$field_langcode = $field['translatable'] ? $langcode : LANGUAGE_NONE;
if (!$use_field_translation) {
$entity->{$field_name} = array(
$field_langcode => array(),
);
}
foreach (element_children($data[$field_name]) as $delta) {
$columns = array();
foreach (element_children($data[$field_name][$delta]) as $column) {
if (isset($data[$field_name][$delta][$column]['#translation']['#text'])) {
$columns[$column] = $data[$field_name][$delta][$column]['#translation']['#text'];
}
elseif (isset($data[$field_name][$delta][$column]['#translate']) && $data[$field_name][$delta][$column]['#translate'] == FALSE) {
$columns[$column] = $data[$field_name][$delta][$column]['#text'];
}
}
if (!isset($entity->{$field_name}[$field_langcode][$delta])) {
$entity->{$field_name}[$field_langcode][$delta] = array();
}
$entity->{$field_name}[$field_langcode][$delta] = array_merge($entity->{$field_name}[$field_langcode][$delta], $columns);
}
}
}
}
drupal_alter('tmgmt_field_post_populate_entity', $entity, $entity_type, $data, $langcode);
}