Drupal 8 Migration: Field Updates

By default, the migrate module in Drupal 8 updates existing nodes by completely overwriting all the fields in the target node with the data from the migration. But what if your client has modified content on the target (D8) system, and still wants to update the content with newly-mapped fields? For instance, there is a “description” that has been updated after the first migration, but now the client wants “subtitle” field to be migrated (which was empty or not present during the first pass), without overwriting the description field changes.

The safest option here would be updating nodes not on a “node by node” basis, but, on a “field by field” one. It is easily done with a slightly modified destination plugin.

Firstly, in order to minimize the effort, we have to extend the base migration class

Secondly, we have to override the wrapper static class “create”, in order to make sure that the plugin is fully compatible with the default “entity:node” handler:

The last bit is actually overriding the  updateEntity method, that handles the merge logic. Most of is is copied from the base class, with a slight modification:

As a summary, here is the full code for the plugin: