zoukankan      html  css  js  c++  java
  • [转]drupal用程序创建node的方法

    function createNode($data = array(), $filepath = NULL) {
        //private function,not necessary to read/copy
        //the
        $node = new stdClass;
        $node->type = "story";
        $node->nid = 0;
        $node->uid = 1;
        $node->status = 1;
        $node->promote = 1;
        $node->changed = $node->created = time();
        $node->sticky = 0;
        $node->format=2;
        $node->title = 'demo node';
        $node->body = 'demo content';
        //$node->taxonomy=array(1,2);
        
        foreach($data as $key => $val) {
            $node->$key = $val;
        }
        
        if($filepath) {
            $file=_ufileobj($filepath);
            $filearray=_cck_filepre($file);
            $node->field_image[0]= $filearray;   // 文件字段,手工改动
        }
        node_save($node);
        $nid=$node->nid;
        $node=NULL;
        return $nid;
    }
    
    function _ufileobj($filepath) {
    // Create a new file record --object
    //print_r(ufileobj( file_directory_path()."/test/test1.flv" ));
        $file = new stdClass();
        //$file->fid=NULL;//when upload success , will return
        $file->list = 1;
        //data igore
        $file->uid = $uid;
        $file->filename = basename($filepath);
        $file->filepath = $filepath;
        $file->filemime = file_get_mimetype(basename($filepath));
        $file->filesize = filesize($filepath);
        // You can change this to the UID you want
        
        $file->status = 1;
        $file->timestamp = time();
        $file->new = true;//addition field , seems useless
        
        drupal_write_record('files', $file);
        // file_set_status($file,1);
        //$node->files[$file_obj->fid] = $file_obj;
        return $file;
    }
    
    function _cck_filepre($file) {
        //return file array which suits drupal cck file field
        //$file is the result ofufileobj($filepath);
        $filearray=array();
        $filearray[fid]=$file->fid;
        $filearray["list"]=1;
        $filearray[data]=array("description"=>"","alt"=>"","title"=>"");
        $filearray[uid]=1;
        $filearray[filename]=$file->filename;
        $filearray[filepath]=$file->filepath;
        $filearray[filemime]=$file->filemime;
        $filearray[filesize]=$file->filesize;
        $filearray[status]=1;
        $filearray[timestamp]=$file->timestamp;
        return $filearray;
    }
    
  • 相关阅读:
    通过代码学REST之二——Restlet框架学习
    页面解析工具:HtmlParser学习
    游标的使用
    软件测试工具杂谈
    XUL资料
    MYSQL5.1修改表名与复制表结构的定时器与存储过程
    mysql 5.7以上版本下载及安装
    AnyChart图表控件(一)简介
    AnyChart图表控件(二)优势
    踩坑 Pycharm 2020.1.1 安装/ JetBrains破解/ anacode配置
  • 原文地址:https://www.cnblogs.com/catcat811/p/1951642.html
Copyright © 2011-2022 走看看