プログラムでグループノードを作成し、そのグループにユーザーを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"));
}
私はそれを考え出した。グループ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"));
}
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"));
}
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);