I'm writing some migrations classes using Migrate module. Some of my source fields does not have data for migration but the Migrate module creates Drupal fields with null values anyway. How to prevent that? I only want to create fields if there are non-null values for them.

This comment suggests that it should be enough to just unset field from source row (for example inside prepareRow), but that doesn't work for me - the field with null value is created anyway.

1 Answer 1


Apparently this feature was added to Migrate 2.6 and for now is available only when you're using dev version. For those who want to try it out, there is RC1 version available.

For older Migrate module versions, the empty field needs to be unset in prepare function, not inside prepareRow as the comment from drupal docs page suggested. Here is an example:

function prepare($entity, $row) {
  // Assuming that 'year' maps to 'field_year'
  if (!isset($row->year) || $row->year === null)

  // Assuming that 'gender' maps to 'field_gender'
  if (!isset($row->gender) || $row->gender === null)

There is also a more "generic" solution prosed in one of the comments on drupal.org (pasting it here for future reference):

 * Removes empty values prior to migrating.
 * @see http://drupal.org/node/1665332
 * @see http://drupal.org/node/1792894
function mymodule_remove_empties($destination, $entity) {
  if ($destination instanceof MigrateDestinationEntity) {
    $entity_type = $destination->getEntityType();
    $form = $form_state = array();
    _field_invoke_default('submit', $entity_type, $entity, $form, $form_state);

And in each of your Migrate classes:

public function prepare($entity, stdClass $source_row) {
  mymodule_remove_empties($this->destination, $entity);

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.