10

I am trying to programmatically create a group node and add a user to that group in Drupal 7. The group node is being created just fine but the user is not being added to the group and I am not getting any errors. I am guessing I am using the og_group function incorrectly but I am not sure. What am I doing wrong?

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"));
}
  • hi max - you raised a good question . thx alot – zero Mar 25 '12 at 10:57
13

I figured it out. It ended up not working because the group ID is NOT the same as the node ID for that organic group. Here is the working version:

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"));
}
| improve this answer | |
13

Since OG7-2.x the node ID == group ID, there is no need to use og_get_group(). And in og_group() and og_role_grant() your group type is the first argument. So here is the same code for 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"));

}

| improve this answer | |
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. – Chapabu Nov 30 '12 at 10:05
  • 2
    Sorry if I did something wrong. I believe I provide an answer for the people who come here through a search engine and are using 7.x-2.x. You may delete the entire post if it doesn't make sence here. – Capono Nov 30 '12 at 10:49
  • Your answers is a good start, but pointing out what is wrong in the question isn't enough for this to be considered an answer. Please revise the text to be more helpful by telling people what to do instead of using og_get_group, and the downvotes will likely be converted to up-votes instead. :) – Letharion Nov 30 '12 at 14:14
  • Ok, I edited my post. I guess this is what you mean? – Capono Nov 30 '12 at 19:40
  • 1
    This works well with 7.2.x. As mentioned the 7.1.x had this og_get_group function but that has been removed in 7.2.x. So for those looking for the later, please use this. – Gladiator Jan 27 '14 at 13:14
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);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.