I've created a small library of functions, which help me with common tasks that I need to do repetitively within prepare hooks in my migrate scripts. These tasks include:

  • Mapping taxonomy term names to tids
  • Mapping node titles to nids
  • Preparing nodereference array structures
  • Preparing filefield/imagefield array structures
  • Copying and saving files
  • Generating random strings

My function library is attached here, as a patch which adds a new file to migrate_extras called migrate_extras.snippets.inc.

Some sample code of how I use this function library in a custom module:

/**
 * Implementation of hook_migrate_prepare_user().
 */
function migrate_dummy_migrate_prepare_user(&$account, $tblinfo, $row) {
  $errors = array();

  switch ($tblinfo->view_name) {
    case 'dummydata_people':
      module_load_include('inc', 'migrate_extras', 'migrate_extras.snippets');

      // snip

      $account['pass'] = migrate_extras_rand_str();

      // snip

      if ($tid = migrate_extras_lookup_taxonomy_tid($row->dummydata_people_tags, array(1))) {
        $account['profile|taxonomy'][1] = array($tid);
      }

      // snip

      if ($nid = migrate_extras_lookup_node_nid($row->dummydata_people_foo, array('foo'))) {
        $account['profile|field_foo_nid'] = $nid;
      }

      // snip

      $refs = migrate_extras_prepare_nodereferences($row->dummydata_people_bar, array('bar'));
      if (!empty($refs)) {
        $account['profile|field_bar'] = $refs;
      }

      // snip

      $src_dir = 'import/images/profile';
      $dest_dir = variable_get('file_directory_path', 'files') . '/images/profile';
      $filename = $row->dummydata_people_image;
      migrate_extras_copy_files($src_dir, $dest_dir);
      if ($image = migrate_extras_prepare_filefield($filename, $dest_dir, $name)) {
        $account['profile|field_profile_image'][0] = $image;
      }
      else {
        $errors[] = array('level' => MIGRATE_MESSAGE_NOTICE, 'message' => t('Failed to find profile image file %file, not importing record with id %id.', array('%file' => $filename, '%id' => $row->dummydata_people_legacyid)));
      }

      break;
  }

  return $errors;
}

/**
 * Implementation of hook_migrate_prepare_user().
 */
function migrate_dummy_migrate_complete_user(&$account, $tblinfo, $row) {
  $errors = array();

  switch ($tblinfo->view_name) {
    case 'dummydata_people':
      module_load_include('inc', 'migrate_extras', 'migrate_extras.snippets');

      $dest_dir = variable_get('file_directory_path', 'files') . '/images/profile';
      migrate_extras_complete_filefield($row->dummydata_people_image, $account->uid, $dest_dir);

      break;
  }

  return $errors;
}

I think this would be a very useful addition to the migrate_extras module. Hoping you will consider adding my include file to the module.

CommentFileSizeAuthor
migrate_extras_snippets.patch6.68 KBJaza

Comments

Jaza’s picture

Note: the sample code above (and, basically, the snippets library) depends on these two patches:

#684232: Remove content_profile integration from 2.x
#685256: md5pass feature causes fatal errors

edrauta’s picture

Component: migrate_extras » Migrate Extras Features

Subscribe

how is file snippet?

you can upload de module?

eloivaque’s picture

I need the code nodereference to multiple values

/**
 * Prepares a semicolon-separated list of node titles, by mapping each title to
 * a node ID, and producing an array ready to be used for a nodereference
 * property of a node object.
 *
 * @param $titles
 *   Semicolon-separated node titles in a string.
 * @param $types
 *   Node types that the node titles may be.
 *
 * @return
 *   Nested array in the form:
 *   array(
 *     array(
 *       'nid' => 123
 *     ),
 *     array(
 *       'nid' => 456
 *     )
 *  )
 */
function migrate_extras_prepare_nodereferences($titles, $types = array()) {
  $ret = array();

  if (!empty($titles)) {
    foreach (explode(';', $titles) as $title) {
      if ($nid = migrate_extras_lookup_node_nid($title, $types)) {
        $ret[]['nid'] = $nid;
      }
    }
  }

  return $ret;
}

/**
 * Maps a node title to a node ID.
 *
 * @param $title
 *   Node title.
 * @param $types
 *   Array of node types to search. Optional.
 *
 * @return
 *   Node ID if found, otherwise NULL.
 */
function migrate_extras_lookup_node_nid($title, $types = array()) {
  $args = array_merge($types, array($title));
  if ($node = db_fetch_object(db_query("SELECT n.nid FROM {node} n WHERE" . (!empty($types) ? " n.type IN(" . db_placeholders($types, 'varchar') . ") AND" : '') . " n.title = '%s'", $args))) {
    return $node->nid;
  }
}

But I not where to put the inside the module of migrate_extras, can you help me please? :)

mikeryan’s picture

Component: Migrate Extras Features » Documentation
Assigned: Jaza » Unassigned
Category: feature » task
Status: Needs review » Closed (won't fix)

Migrate and Migrate Extras V1 are no longer supported, so these snippets are obsolete. For Migrate V2, please submit useful snippets to the Cookbook in the documentation at http://drupal.org/node/1007002.