Drupal 7 开发内部培训资料, 分头诗人

Drupal 7 如何通过代码创建nodes, comments, taxonomies

猪跑啦独家原创专稿,欢迎您转载本文,转载请注明来源。

 

1,创建一个node节点

1.1 初始化 node object

1
2
3
4
5
6
7
$node = new stdClass(); // 创建一个node object
$node->type = "page"; // 要保存的内容类型是 page,也可以是其它
$node->title = "文章标题";
$node->language = LANGUAGE_NONE; // 或者是其他开启了的语言
$node->uid = 1; // 用户的id,也可以是设置其他用户的id
$node->path = array('alias' => 'your node path'); // 设置一个节点的路径
node_object_prepare($node); // 设置默认值.

LANGUAGE_NONE 为不限语言。drupal里面,node跟field是可以有多语言的,如果系统有多语言,可以在Configuration -> Regional and language -> Languages 设置语言。

1.2 增加 body 内容

1
2
3
4
// 增加body 内容
$node->body[$node->language][]['value'] = '这是body内容';
$node->body[$node->language][]['summary'] = '这是body摘要';
$node->body[$node->language][]['format'] = 'filtered_html'; // 如果body需要文本格式,可以通过这里设置

 

1.3 自定义字段的内容增加方法

1
2
3
4
5
// 增加一些非默认的字段
$node->field_custom_name[$node->language][]['value'] = '这是一个默认字段的值';
// 如果需要文本格式,可以通过这里设置
$node->field_custom_name[$node->language][]['format'] = 'This is a custom field value';
// 可以通过以上方法增加其他自定义字段值 ;)

 

1.4 增加 file/image字段

1
2
3
4
5
6
7
8
9
10
// Some file on our system
$file_path = drupal_realpath('somefile.png'); // Create a File object
$file = (object) array(
          'uid' => 1,
          'uri' => $file_path,
          'filemime' => file_get_mimetype($filepath),
          'status' => 1,
);
$file = file_copy($file, 'public://'); // Save the file to the root of the files directory. You can specify a subdirectory, for example, 'public://images'
$node->field_image[LANGUAGE_NONE][] = (array)$file; //associate the file object with the image field:

 

1.5 给一个node增加分类

1
$node->field_tags[$node->language][]['tid'] = 1;

 

1.6 保存 node

1
2
$node = node_submit($node); // Prepare node for a submit
node_save($node); // After this call we'll get a nid

 

2. 创建一个comment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Let's create a managed object $comment = new stdClass(); // We create a new comment object
$comment->nid = $node->nid; // nid of a node you want to attach a comment to
$comment->cid = ; // leave it as is
$comment->pid = ; // parent comment id, 0 if none
$comment->uid = 1; // user's id, who left the comment
$comment->mail = 'email@example.com'; // user's email
$comment->name = 'User name'; // If user is authenticated you can omit this field, it will be auto-populated, if the user is anonymous and you want to name him somehow, input his name here
$comment->thread = '01/'; // OPTIONAL. If you need comments to be threaded you can fill this value. Otherwise omit it.
$comment->hostname = '127.0.01' // OPTIONAL. You can log poster's ip here
$comment->created = time(); // OPTIONAL. You can set any time you want here. Useful for backdated comments creation.
$comment->is_anonymous = ; // leave it as is
$comment->homepage = ''; // you can add homepage URL here
$comment->status = COMMENT_PUBLISHED; // We auto-publish this comment
$comment->language = LANGUAGE_NONE; // The same as for a node
$comment->subject = 'Comment subject';
$comment->comment_body[$comment->language][]['value'] = 'Comment body text'; // Everything here is pretty much like with a node
$comment->comment_body[$comment->language][]['format'] = 'filtered_html';
$comment->field_custom_field_name[LANGUAGE_NONE][]['value'] = 'Some value'; // OPTIONAL. If your comment has a custom field attached it can added as simple as this // preparing a comment for a save
comment_submit($comment); // saving a comment
comment_save($comment);

3. 创建一个taxonomy term

1
2
3
4
5
$term = new stdClass();
$term->name = 'Term Name';
$term->vid = 1; // '1' is a vocabulary id you wish this term to assign to
$term->field_custom_field_name[LANGUAGE_NONE][]['value'] = 'Some value'; // OPTIONAL. If your term has a custom field attached it can added as simple as this
taxonomy_term_save($term); // Finally, save our term