分类数据批量导入模块

-- Submitted by Admin on Tue, 07/20/2010 - 22:22

最近的一个项目中,客户给了大量的数据用作分类,一个一个添加肯定要麻烦死,于是想到要批量添加才好,就写了这个模块,果然方便,哈哈!

模块包见附件,希望对大家的开发有所帮助!

使用方法: 访问 admin/import_term 路径就看到了。

这里贴出核心函数:

<?php
function import_term_form_submit($form, &$form_state) {
   
//取出词汇表vid和父分类tid
   
$vid = trim($form_state['values']['vid']);
   
$parent_tid = trim($form_state['values']['parent_tid']);
   
//取出terms的值,转换成数组
   
$terms = explode("\r\n", $form_state['values']['terms']);
   
   
//保存分类
   
if(!empty($terms)) {
        foreach(
$terms as $value) {
           
//先取消变量$term定义
           
unset($term);
           
//去掉字符串前后空白字符
           
$value = trim($value);

            if(
$value != null) {
               
$term = array(
                   
'name' => $value,
                   
'vid' => $vid,
                   
'parent' => $parent_tid,
                );
               
taxonomy_save_term($term);
            }
        }
       
drupal_set_message('导入分类成功!');
    }
}
?>

PreviewAttachmentSize
term_import.rar1.18 KB

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Allowed HTML tags: <a> <em> <strong> <code> <cite> <ul> <ol> <li> <dl> <dt> <dd> <img> <ebmed>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
3 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.