如何在Drupal 7中以编程方式将用户添加到组


10

我正在尝试以编程方式创建一个组节点,并在Drupal 7中将用户添加到该组中。正在创建组节点,但是没有将用户添加到该组中,并且没有出现任何错误。我猜我使用的og_group函数不正确,但是我不确定。我究竟做错了什么?

function MYMODULE_form_submit($form_id, $form_values) {
    global $user;

    $node = new stdClass();

    $node->type     = "group";
    $node->uid      = $user->uid;
    $node->title        = t("Group Node Title");
    $node->body     = t("Group Node Body");
    $node->status       = 1;
    $node->promote      = 0;
    $node->comment      = 1;

    $node->og_description   = t("OG Description");
    $node->og_register  = 0;
    $node->og_directory = 0;
    $node->og_private   = 1;
    $node->og_selective = 3;

    $node = node_submit($node);
    node_save($node);

    $account = user_load(2);

    og_group($node->nid, array(
                "entity type"       => "user",
                "entity"        => $account,
                "membership type"   => "OG_MEMBERSHIP_TYPE_DEFAULT",
            ));

    drupal_set_message(t("Finished"));
}

嗨,麦克斯-您提出了一个很好的问题。thx很多

Answers:


13

我想到了。由于组ID与该有机组的节点ID不同,最终无法正常工作。这是工作版本:

function MYMODULE_page_form_submit($form_id, $form_values) {
    global $user;

    $node = new stdClass();

    $node->type     = "group";
    $node->uid      = $user->uid;
    $node->title        = t("Group Node Title");
    $node->body     = t("Group Node Body");
    $node->status       = 1; //(1 or 0): published or not
    $node->promote      = 0; //(1 or 0): promoted to front page
    $node->comment      = 1; //2 = comments on, 1 = comments off

    $node->og_description   = t("OD Description");
    $node->og_register  = 0;
    $node->og_directory = 0;
    $node->og_private   = 1;
    $node->og_selective = 3;

    $node = node_submit($node);
    node_save($node);

    // Get the group ID from the node ID
    $group = og_get_group("node", $node->nid);

    // Load the user we want to add to the group (ID #2 was my test user)
    $account = user_load(2);

    // Add the user to the group
    og_group($group->gid, array(
                "entity type"       => "user",
                "entity"        => $account,
                "membership type"   => OG_MEMBERSHIP_TYPE_DEFAULT,
            ));

    // Changes the users role in the group (1 = non-member, 2 = member, 3 = administrator member)
    og_role_grant($group->gid, $account->uid, 3);

    drupal_set_message(t("Finished"));
}

13

由于OG7-2.x的节点ID ==组ID,因此无需使用og_get_group()。在og_group()和og_role_grant()中,您的组类型是第一个参数。所以这是OG 7.x-2.x的相同代码

function MYMODULE_page_form_submit($form_id, $form_values) {
global $user;

$node = new stdClass();

$node->type     = "group";
$node->uid      = $user->uid;
$node->title        = t("Group Node Title");
$node->body     = t("Group Node Body");
$node->status       = 1; //(1 or 0): published or not
$node->promote      = 0; //(1 or 0): promoted to front page
$node->comment      = 1; //2 = comments on, 1 = comments off

$node->og_description   = t("OD Description");
$node->og_register  = 0;
$node->og_directory = 0;
$node->og_private   = 1;
$node->og_selective = 3;

$node = node_submit($node);
node_save($node);

// Load the user we want to add to the group (ID #2 was my test user)
$account = user_load(2);

// Add the user to the group
og_group('node', $node->nid, array(
            "entity type"       => "user",
            "entity"        => $account,
            "membership type"   => OG_MEMBERSHIP_TYPE_DEFAULT,
        ));

// Changes the users role in the group (1 = non-member, 2 = member, 3 = administrator member)
og_role_grant('node', $node->nid, $account->uid, 3);

drupal_set_message(t("Finished"));

}


这不能为问题提供答案。要批评或要求作者澄清,请在其帖子下方留下评论-您可以随时评论自己的帖子,一旦您拥有足够的声誉,就可以在任何帖子中发表评论
Chapabu 2012年

2
对不起,如果我做错了。我相信我为通过搜索引擎来到这里并使用7.x-2.x的人们提供了答案。如果此处没有任何意义,您可以删除整个帖子。
卡波诺2012年

您的答案是一个好的开始,但是指出问题中的错误不足以使它被视为答案。请通过告诉人们该怎么做而不是使用og_get_group来修改文本,以提供更多帮助,并且降低投票数可能会转换为向上投票数。:)
Letharion 2012年

好的,我编辑了我的帖子。我想这就是你的意思?
卡波诺2012年

1
这在7.2.x上很好用。如前所述,7.1.x具有此og_get_group函数,但已在7.2.x中删除。因此,对于那些寻找以后的人,请使用它。
角斗士2014年

1
Adding programmatically Group  content:
$node->type     = "group_post";
$node->uid      = $user->uid;
$node->title        = t("Group postNode Title");
$node->body     = t("Group Node Body");
$node->status       = 1; //(1 or 0): published or not
$node->promote      = 0; //(1 or 0): promoted to front page
$node->comment      = 1; //2 = comments on, 1 = comments off

$node->og_group_ref['und'][] = array('target_id' => $gid);

$node = node_submit($node);
node_save($node);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.