You are here

public function MigrateTextFieldHandler::prepare in Migrate 6.2

Same name and namespace in other branches
  1. 7.2 plugins/destinations/fields.inc \MigrateTextFieldHandler::prepare()

File

plugins/destinations/fields.inc, line 190
Support for processing CCK fields

Class

MigrateTextFieldHandler

Code

public function prepare($entity, array $instance, array $values) {
  if (isset($values['arguments'])) {
    $arguments = $values['arguments'];
    unset($values['arguments']);
  }
  else {
    $arguments = array();
  }
  $migration = Migration::currentMigration();
  $destination = $migration
    ->getDestination();
  $max_length = isset($instance['max_length']) ? $instance['max_length'] : 0;

  // Setup the standard Field API array for saving.
  $delta = 0;
  foreach ($values as $value) {
    $item = array();
    if (isset($arguments['format'])) {
      if (is_array($arguments['format'])) {
        $format = $arguments['format'][$delta];
      }
      else {
        $format = $arguments['format'];
      }
    }
    else {
      $format = $destination
        ->getTextFormat();
    }
    $item['format'] = $item['value_format'] = $format;

    // Make sure the value will fit
    if ($max_length) {
      $item['value'] = drupal_substr($value, 0, $max_length);
      if (!empty($arguments['track_overflow'])) {
        $value_length = drupal_strlen($value);
        if ($value_length > $max_length) {
          $migration
            ->saveMessage(t('Value for field !field exceeds max length of !max_length, actual length is !length', array(
            '!field' => $instance['field_name'],
            '!max_length' => $max_length,
            '!length' => $value_length,
          )), Migration::MESSAGE_INFORMATIONAL);
        }
      }
    }
    else {
      $item['value'] = $value;
    }
    $return[$delta] = $item;
    $delta++;
  }
  return isset($return) ? $return : NULL;
}