

לאחרונה כתבתי מודול קטנטן שתפקידו לטעון פריטי תוכן מסוג מסוים לתוך בסיס הנתונים של דרופל. פריטי התוכן נלקחים מתוך קובץ CSV.
השאלה שעולה היא כמובן מדוע לא להשתמש ב import_node. ובכן ניסיתי... נראה שimport_node נותן מענה לתרחישי טעינה בסיסיים אך במקרה שלי כיון שהשתמשתי בcontent_taxonomy וכן בauto_nodetitle הטעינה הסתיימה כל פעם באיכלוס חלקי של פריט התוכן.
המודול משתמש בfgetcsv של PHP כקלט ובdrupal_execute כאמצעי הרישום. יש אסכולות שונות בנוגע לשימוש בdrupal-execute מול node_save. לי נראה שככל שמודול הטעינה נשאר מחקה טוב יותר את תרחיש הGUI (קרי טעינת ערכי הטופס), הייתכנות ליצירת חוסר קונסיסטנטיות בבסיס הנתונים נמוכה יותר. עם זאת המסתייגים מצביעים על כך שקוד הולידציה אינו מופעל בשיטה זו.
להלן הקוד:
<?php
// $id$
/**
* Valid permissions for this module
* @return array An array of valid permissions for the import content module
*/
function import_content_perm() {
return array('import content');
} // function node_order_perm()
// need to add a menu entry that will direct to the path import_content
function import_content_menu($may_cache){
$items=array();
if($may_cache){
$items[]=array(
'path' => 'import_content',
'callback' => 'import_content_start',
'type' => MENU_CALLBACK,
'access' => user_access('import content'),
);
}
return $items;
}
function import_content_start(){
$node_type = 'business_card';
$csv_file_name = 'bizcard.csv';
// disabling momentarily the auto_nodetitle module which does not cooperate with drupal_execute
if (module_exists('auto_nodetitle')){
$auto_nodetitle_state = variable_get('ant_'. $node_type, AUTO_NODETITLE_DISABLED);
variable_set('ant_'. $node_type, AUTO_NODETITLE_DISABLED);
}
// the csv file should be located on Drupal's root
setlocale(LC_ALL, 'en_US.UTF-8');
$handle = fopen ('./'.$csv_file_name, 'r');
while (($data = fgetcsv($handle, 1000, ',', '"')) !== FALSE){
$node = array('type' => $node_type); // a variable holding the content type
// now we build the form values manually mapping the CSV into the form fields
$values = array();
$values['title'] = $data[0];
$values['field_business_name'][0]['value'] = $data[0];
$values['field_category']['tids']= $data[1]; // this is a field based on content taxonomy (integartion of CCK with Taxonomy)
$values['field_website'][0]['url'] = $data[2];
$values['field_portfolio'][0]['url'] = $data[3];
$values['status'] = 1;
$values['type'] = $node_type; // the node's type
// 'recipe_node_form' would be 'story_node_form' or
// whatever node type you're creating.
$errs = drupal_execute('business_card_node_form', $values, (object)$node);
/*
if (count($errs)) {
print_r($errs);
}
*/
}
// restore the auto_nodetitle settings
if (module_exists('auto_nodetitle')){
variable_set('ant_'. $node_type, $auto_nodetitle_state);
}
drupal_goto('/'); // goto home;
}
?>כדי להפעיל את הטעינה במקרה זה יש להתקין את המודול ולקרוא ל- URL:http://www.mysite.com/import_content
לפני כן יש לשים לב:
1. קובץ הCSV צריך להיות ממוקם בroot של Drupal
2. הקובץ צריך לעבור תחילה המרה לcsv ואח"כ המרה נוספת לUTF-8 במידה ונעשה שימוש בעברית. המרה זו מתבצעת באמצעות notepad (שמור בשם + פורמט UTF-8)
3. יש לקבוע את שם הקובץ וסוג פריט התוכן בראש פונקצית הטעינה. כמו כן יש לבצע את המיפוי לשדות בגוף הפונקציה
4. במידה ויש קושי לזהות שדות מסויימים, ניתן להדפיס את שדות הטופס בפונקצית הvalidate של הטופס. למשל