diff --git a/entities/user.inc b/entities/user.inc new file mode 100644 index 0000000..fa502a5 --- /dev/null +++ b/entities/user.inc @@ -0,0 +1,171 @@ +map = new MigrateSQLMap($arguments['machine_name'], + array( + 'uuid' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'description' => 'Source UUID', + 'alias' => 'uuuid', + ), + ), + MigrateDestinationUser::getKeySchema() + ); + parent::__construct($arguments); + if (isset($map)) { + $this->map = $map; + } + $this->source = new MigrateSourcePHP($arguments['source_file']); + if ($this->source->computeCount() == 0) { + return; + } + + $common = new MigrateDefaultContentCommon(); + // Assume a 1:1 mapping of fieldnames. + $common->setFieldMapping($this); + + // @TODO user->uid / user references + // @TODO find some way to document unmapped pseudo-fields + // Do not map the uid from source to the destination, they shouldn't need to be the same. + $this->removeFieldMapping('uid'); + } + + /** + * @param $row + * + * @return bool|void + */ + public function prepareRow($row) { + $common = new MigrateDefaultContentCommon(); + + // Map the user's roles. + // Import roles via features. + $source_id = $row->uid; + $role_names = array_values($row->roles); + $query = db_select('role', 'r') + ->fields('r', array('rid', 'name')) + ->condition('r.name', $role_names, 'IN'); + $results = $query->execute(); + + // Add the authenticated role. All users have this role. + $roles = array('2' => '2'); + foreach ($results as $result_row) { + $roles[$result_row->rid] = $result_row->rid; + } + $row->roles = $roles; + + // Remove the path array if pathauto = 1 to prevent double url aliases for entity + if (isset($row->path) && isset($row->pathauto) && $row->pathauto) { + unset($row->path); + } + + foreach ($row as $field_name => $field_value) { + $common->prepareField($row->{$field_name}, $field_name, $this); + } + } + + /** + * @param $entity_id + */ + public function prepareRollback($entity_id) { + if (!is_array($entity_id)) { + $entity_id = array($entity_id); + } + $entity_uuid = entity_get_uuid_by_id('user', $entity_id); + // Code to execute before an entity has been rolled back + // Remove references that don't need resolving anymore + $existing_references = variable_get('defaultcontent_references', array()); + foreach ($existing_references as $target_id => $references) { + foreach ($references as $key => $reference) { + if ($reference['source_uuid'] == current($entity_uuid)) { + unset($existing_references[$target_id][$key]); + } + } + if (empty($existing_references[$target_id])) { + unset($existing_references[$target_id]); + } + } + variable_set('defaultcontent_references', $existing_references); + } + + /** + * @param $account + * @param $row + */ + public function complete($account, $row) { + parent::complete($account, $row); + // Store unresolved referenced, to be retreived in later migrations + $existing_references = variable_get('defaultcontent_references', array()); + // Now that we know the uuid of this entity, add it to the recorded references + if (!empty($this->references)) { + foreach ($this->references as $target_id => $references) { + foreach ($references as $key => &$reference) { + $reference['source_uuid'] = $entity->uuid; + $existing_references[$target_id][] = $reference; + } + } + variable_set('defaultcontent_references', $existing_references); + } + if (!empty($existing_references)) { + // Attempt to resolve references + $this->resolveReferences($account, $existing_references); + } + } + + /** + * Given an array of reference-info for a particular field, adds this + * to the references array. + * @param $reference + * @internal param $references + */ + public function addReference($reference) { + $this->references[$reference['target_id']][] = $reference; + } + + + /** + * Given an entity and an array of unresolved references, add references + * to that entity to previously migrated objects. + * + * @param $entity + * The fully migrated entity object + * @param $references + * The array of references to check + */ + private function resolveReferences($entity, $references) { + if (isset($references[$entity->uuid])) { + foreach ($references[$entity->uuid] as $reference) { + // Entity_uuid_load changes all references to normal IDs back to uuid, + // so we need to fetch the entity using the normal entity_load instead + $ids = entity_get_id_by_uuid($reference['entity_type'], array($reference['source_uuid'])); + $referencing_entity = reset(entity_load($reference['entity_type'], $ids)); + if (empty($referencing_entity)) { + return; + } + $entity_wrapper = entity_metadata_wrapper('user', $entity); + $re_wrapper = entity_metadata_wrapper($reference['entity_type'], $referencing_entity); + $id = $entity_wrapper->getIdentifier(); + if ($re_wrapper->{$reference['field_name']} instanceof EntityDrupalWrapper) { + $re_wrapper->{$reference['field_name']} = intval($id); + } + else { + $re_wrapper->{$reference['field_name']}[$reference['field_delta']] = intval($id); + } + $re_wrapper->save(); + } + unset($references[$entity->uuid]); + unset($this->references[$entity->uuid]); + variable_set('defaultcontent_references', $references); + } + } +} diff --git a/examples/user.inc b/examples/user.inc new file mode 100644 index 0000000..94e9b07 --- /dev/null +++ b/examples/user.inc @@ -0,0 +1,24 @@ + (object) array( + 'uid' => '1', + 'name' => 'exampleuser', + 'pass' => '', + 'mail' => 'exampleuser@localhost.localdomain', + 'theme' => '', + 'signature' => '', + 'signature_format' => 'wysiwyg_html', + 'created' => '1418615300', + 'access' => '0', + 'login' => '0', + 'status' => '0', + 'timezone' => NULL, + 'language' => '', + 'picture' => NULL, + 'init' => 'exampleuser@localhost.localdomain', + 'data' => FALSE, + 'uuid' => '64ac1442-e454-4112-974c-87481050e2d5', + 'roles' => array(), + ), +); diff --git a/migrate_defaultcontent.info b/migrate_defaultcontent.info index 01a1eac..fa3853d 100644 --- a/migrate_defaultcontent.info +++ b/migrate_defaultcontent.info @@ -11,5 +11,6 @@ files[] = migrate_defaultcontent.migrate.inc files[] = includes/migrate_defaultcontent.common.inc files[] = entities/node.inc files[] = entities/taxonomy_term.inc +files[] = entities/user.inc files[] = entities/field_collection.inc files[] = includes/migrate_source_php.inc