You are here

class MigrateTextFieldHandler in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/destinations/fields.inc \MigrateTextFieldHandler

Hierarchy

Expanded class hierarchy of MigrateTextFieldHandler

1 string reference to 'MigrateTextFieldHandler'
migrate_migrate_api in ./migrate.migrate.inc

File

plugins/destinations/fields.inc, line 316
Support for processing entity fields

View source
class MigrateTextFieldHandler extends MigrateFieldHandler {
  public function __construct() {
    $this
      ->registerTypes(array(
      'text',
      'text_long',
      'text_with_summary',
    ));
  }
  static function arguments($summary = NULL, $format = NULL, $language = NULL) {
    $arguments = array();
    if (!is_null($summary)) {
      $arguments['summary'] = $summary;
    }
    if (!is_null($format)) {
      $arguments['format'] = $format;
    }
    if (!is_null($language)) {
      $arguments['language'] = $language;
    }
    return $arguments;
  }

  /**
   * Implementation of MigrateFieldHandler::fields().
   *
   * @param $type
   *  The field type.
   * @param $instance
   *  Instance info for the field.
   * @param Migration $migration
   *  The migration context for the parent field. We can look at the mappings
   *  and determine which subfields are relevant.
   *
   * @return array
   */
  public function fields($type, $instance, $migration = NULL) {
    $fields = array();
    if ($type == 'text_with_summary') {
      $fields['summary'] = t('Subfield: <a href="@doc">Summary of field contents</a>', array(
        '@doc' => 'http://drupal.org/node/1224042#summary',
      ));
    }
    if ($instance['settings']['text_processing']) {
      $fields['format'] = t('Subfield: <a href="@doc">Text format for the field</a>', array(
        '@doc' => 'http://drupal.org/node/1224042#format',
      ));
    }
    $field = field_info_field($instance['field_name']);
    if (field_is_translatable($instance['entity_type'], $field)) {
      $fields['language'] = t('Subfield: <a href="@doc">Language for the field</a>', array(
        '@doc' => 'http://drupal.org/node/1224042#language',
      ));
    }
    return $fields;
  }
  public function prepare($entity, array $field_info, array $instance, array $values) {
    if (isset($values['arguments'])) {
      $arguments = $values['arguments'];
      unset($values['arguments']);
    }
    else {
      $arguments = array();
    }
    $migration = Migration::currentMigration();
    $destination = $migration
      ->getDestination();
    $language = $this
      ->getFieldLanguage($entity, $field_info, $arguments);
    $max_length = isset($field_info['settings']['max_length']) ? $field_info['settings']['max_length'] : 0;

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

      // If the value is an array, which it might be if the destination entity
      // was loaded via entity_load(), ensure we're handling a string.
      if (is_array($value) && !empty($value[0]) && array_key_exists('value', $value[0])) {
        $value = $value[0]['value'];
      }

      // 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;
      }
      if (is_array($language)) {
        $current_language = $language[$delta];
      }
      else {
        $current_language = $language;
      }
      $return[$current_language][] = $item;
      $delta++;
    }
    return isset($return) ? $return : NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateFieldHandler::getFieldLanguage function Determine the language of the field.
MigrateHandler::$dependencies protected property List of other handler classes which should be invoked before the current one.
MigrateHandler::$typesHandled protected property List of "types" handled by this handler. Depending on the kind of handler, these may be destination types, field types, etc.
MigrateHandler::getDependencies public function
MigrateHandler::getTypesHandled public function
MigrateHandler::handlesType public function Does this handler handle the given type? 1
MigrateHandler::registerTypes protected function Register a list of types handled by this class
MigrateTextFieldHandler::arguments static function
MigrateTextFieldHandler::fields public function Implementation of MigrateFieldHandler::fields().
MigrateTextFieldHandler::prepare public function
MigrateTextFieldHandler::__construct public function Overrides MigrateHandler::__construct