You are here

function tmgmt_field_populate_entity in Translation Management Tool 7

Populates a field on an object with the provided field values.

Parameters

$entity_type: The type of $entity.

$entity: The object to be populated.

$langcode: The field language.

$data: An array of values.

$use_field_translation: TRUE if field translation is being used.

3 calls to tmgmt_field_populate_entity()
field_collection_field_type_tmgmt_populate_entity in sources/field/tmgmt_field.field_collection.inc
Implements hook_tmgmt_field_type_populate_entity().
TMGMTEntitySourcePluginController::saveTranslation in sources/entity/tmgmt_entity.plugin.inc
Saves a translation.
TMGMTNodeSourcePluginController::saveTranslation in sources/node/tmgmt_node.plugin.inc
Saves a translation.

File

sources/field/tmgmt_field.module, line 99

Code

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;

        // When not using field translation, make sure we're not storing
        // multiple languages.
        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'];
            }
          }

          // Make sure the array_merge() gets an array as a first parameter.
          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);
}