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

发布于 2024-10-22 19:38:57 字数 368 浏览 5 评论 0原文

我刚刚通过以下链接了解了如何以编程方式创建论坛和容器

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

但从未见过任何以编程方式创建论坛主题的帖子(google) ,我是否应该使用 node_save() 还是任何替代方案。

请帮助我,

谢谢, 埃德温

I just gone through how to create forums and containers programmatically with the below link

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

But never see any post(google) which create forum topic pro-grammatically, whether i should go with node_save() or any alternative.

please help me guys,

Thanks,
Edvin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嗼ふ静 2024-10-29 19:38:57

以编程方式创建新节点的一种快速、安全且简单的方法是使用 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);
?>

A quick, safe and easy way to create new nodes programmatically is to use 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);
?>
小糖芽 2024-10-29 19:38:57

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);

Drupal 7 Examples :

To create new Forum

    $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);

To create new Topic

    $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);

To create topic comments

    $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);
一个人练习一个人 2024-10-29 19:38:57

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

A forum topic is really just a node, so node_save is the best option as all the needed hooks will be called.

围归者 2024-10-29 19:38:57

使用 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();

Using D7 Entity 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文