如何以编程方式创建论坛主题?

时间:2011-03-16 06:04:09

标签: drupal drupal-6 forum drupal-forms

我刚刚介绍了如何使用以下链接以编程方式创建论坛和容器

http://www.unibia.com/unibianet/drupal/how-create-drupal-forums-and-containers-programmatically

但是从来没有看到任何以编程方式创建论坛主题的帖子(谷歌),无论我应该使用node_save()还是其他任何选择。

请帮帮我们,

谢谢, 埃德文

4 个答案:

答案 0 :(得分:4)

以编程方式创建新节点的快速,安全且简便的方法是使用node_save()

<?php
  // Construct the new node object.
  $node = new stdClass();

  // Set the values for the node
  $node->title = "My new forum topic";
  $node->body = "The body of my forum topic.\n\nAdditional Information";
  $node->type = 'forum';   // Your specified content type
  $node->created = time();
  $node->changed = $node->created;
  $node->status = 1;       // To have published, else use 0
  $node->promote = 1;      // If you want promoted to front page, else use 0
  $node->sticky = 0;
  $node->format = 1;       // Filtered HTML
  $node->uid = 1;          // UID of content owner
  $node->language = 'en';

  // If known, the taxonomy TID values can be added as an array.
  $node->taxonomy = array(2,3,1,);

  node_save($node);
?>

答案 1 :(得分:2)

Drupal 7例子:

创建新论坛

    $forum = new stdClass();
    $forum->name = uniqid();
    $forum->description = uniqid();
    $forum->parent = array(0);
    $forum->weight = 0;
    $forum->vid = variable_get('forum_nav_vocabulary', 0);
    taxonomy_term_save($forum);

创建新主题

    $node = new stdClass();
    // Set the values for the node
    $node->title = "My new forum topic";
    $node->body = "The body of my forum topic.\n\nAdditional Information";
    $node->type = 'forum'; // Your specified content type
    $node->created = time();
    $node->changed = $node->created;
    $node->status = 1; // To have published, else use 0
    $node->comment = 2; // To have active, else use 0
    $node->uid = 1; // UID of content owner
    //Creates topic at the first "forum"
    $node->forum_tid = reset(array_keys(taxonomy_term_load_multiple(array(), 
    array('vid' => variable_get('forum_nav_vocabulary', 0))))); 
    $node->language = LANGUAGE_NONE;
    $node->taxonomy_forums[LANGUAGE_NONE][0]['tid'] = $node->forum_tid;
    node_save($node);

创建主题评论

    $comment = (object)array(
            'nid' => $node->nid,
            'pid' => 0,
            'cid' => 0,
            'uid' => 1,
            'mail' => '',
            'is_anonymous' => 0,
            'homepage' => '',
            'status' => COMMENT_PUBLISHED,
            'subject' => 'subject test ' . uniqid(),
            'language' => LANGUAGE_NONE,
            'comment_body' => array(
                LANGUAGE_NONE => array(
                    0 => array(
                        'value' => 'Comment Body ' . uniqid(),
                        'format' => 1
                    )
                )
            ),
        );
     comment_submit($comment);
     comment_save($comment);

答案 2 :(得分:1)

论坛主题实际上只是一个节点,因此node_save是最佳选择,因为将调用所有需要的钩子。

答案 3 :(得分:0)

使用D7实体API

global $user;
$options = array(
'type' => 'forum',
'uid' => $user->uid,
'status' => 1
);
$entity = entity_create('node', $options);
$wrapper = entity_metadata_wrapper('node', $entity);
$wrapper->title = 'Topic Title;
$wrapper->body = 'Topic Body;    
$terms = taxonomy_get_term_by_name ('General discussion','forums'); //names of tid and vid
$wrapper->taxonomy_forums = reset($terms)->tid;
$wrapper->save();