Migrate entity's "changed" time in Drupal 7

Last updated on 13/02/2017

At first, you would expect that by doing something like:

$this->addFieldMapping('changed', 'LastModifiedDate');

would populate the changed time.

But, because the node_save() function always populates it: $node->changed = REQUEST_TIME; it wouldn't have any effect.

Solution:

First, push the desired date in a custom property of the entity object:

  /**
   * Prepare function of the Migration class.
   */
  public function prepare($entity, $row) {
 
    /**
     * We want to push the same Updated date.
     * @see: hook_node_presave();
     */
    $entity->modified_date = strtotime($row->LastModifiedDate);
  }

Then, we can populate changed property of Drupal's entity:

/**
 * Implements hook_entity_presave().
 *
 * Push our own changed time.
 */
function hook_entity_presave($entity, $type) {
  if (!empty($entity->modified_date)) {
    $entity->changed = $entity->modified_date;
  }
}