3

I have a custom migrate import that imports some fields like:

...
$this->addFieldMapping('title', 'title');
$this->addFieldMapping('body', 'description');
$this->addFieldMapping('created', 'created');
...

If I run an update of some items (the source data is a live site) I don't want to update the title or body fields because editors are changing this content in the drupal site.

I'm using this workarround but I'm not sure this is the best way to do this:

/**
 * Migrate prepare method.
 */
public function prepare($entity, $row) {
  if (isset($entity->is_new) && $entity->is_new === FALSE) {
    unset($entity->body);
    unset($entity->title);
  }
}

Thanks!

2
  • Just to be clear before I answer, you want to only exclude those two fields; not the skip the whole row update, correct?
    – webkenny
    Commented Dec 24, 2014 at 13:00
  • Yes this two fields, but only the updated ones, the new ones should map all fields.
    – jorgetutor
    Commented Dec 24, 2014 at 13:01

1 Answer 1

4

Good news! You are doing this exactly right. By "unsetting" the fields in the source data you are taking the proper approach. This practice is common and the relative snippet of documentation is buried a bit in the Migrate Handbook:

To use default values for NULL or '' or array() values, be sure to remove the item with unset() in your prepareRow() [or prepare()] function.

From https://www.drupal.org/node/1133448

Good luck with the migration!

1
  • 1
    I confirmed this approach works, however I needed the additional step of $entity->revision = 0 in prepare(). The node type I was importing to had revisions enabled - I suspect this line was only needed because of that. Without setting $entity->revision = 0 I would get a new revision saved, and the title and body fields would become blank in the new revision. Commented Apr 21, 2016 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.