Last updated February 6, 2014. Created on October 16, 2013.
Edited by LeeHunter, nehajn, 999csharp. Log in to edit this page.

This is a drupal notebook for helping users and developers in various sections.

Custom Code to import rows from other database to drupal site for custom content type.

<?php
function mycustom_migrate_init() {
  
module_load_include('inc', 'mycustom_migrate', 'connect');
  
variable_set('taxonomyMacineName','toc');
  
variable_set('vid',11);
  }


function
mycustom_migrate_menu() {
  
$menuitems['admin/migrate/links'] = array(
   
'title' => 'Links',
   
'page callback' => 'drupal_get_form',
   
'access callback' => TRUE,
   
'page arguments' => array('process_node_form'),
   
'type' => MENU_NORMAL_ITEM,    
    );
     return
$menuitems;

}  


function
process_node_form($form, $form_state) {
 
$form = array();
  
 
 
$form['submit'] = array(
     
'#type' => 'submit',
     
'#value' => 'Import Node',
    );
  return
$form ;
}
 
function
process_node_form_submit($form, $form_state) {  
  global
$user;
  global
$con;
 
if(
$con)
  {  
    
$query_top = "SELECT * FROM `tbl_link` limit  1,100";
    
$result = mysql_query($query_top);  
      while (
$row = mysql_fetch_assoc($result)) {             
        
$taxonomy_macine_name = 'toc';
        
$tid  getTermId($row['parentTopic']);      
        
$node = new stdClass();
        
$node->language = 'und';        
        
$node->type = 'link';
        
node_object_prepare($node);
        
$node->title = $row['title'];
        
$node->body['und'][0]['value']   = $row['description'];           
        
$node->field_link[$node->language][0]['value'] = $row['page'];
        
$node->field_link_type[$node->language][0]['value'] = 'Regular';
        
$node->field_priority[$node->language][0]['value'] = $field_priority;
        
$node->uid = $user->uid;
        
$node->body[$node->language][0]['format']  = 'full_html';
        
$node->created = strtotime("now");
        
$node->field_category['und'][0] = array('tid' =>$tid);
        
$node->comment = 0;
        
$node->promote = 0;
        
$node->moderate = 0;
        
$node->sticky = 0;
        
$node = node_submit($node); // Prepare node for saving
        
node_save($node);
         }
   
drupal_set_message('Node saved.');
}

}

function
getTermId($catPath)
{
   
$taxonomyMacineName = variable_get('taxonomyMacineName');
   
$termNames = explode('/',$catPath); 
   
$keys = array_keys($termNames);
   
$last = end($keys);
   
$termName = $termNames[$last];
   
$term = taxonomy_get_term_by_name($termName,$taxonomyMacineName);       
   
$keykey($term);
   
$tid = $term[$key]->tid;
   
    return
$tid
}
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.