it-swarm.com.de

So fügen Sie einen Benutzer programmgesteuert zu einer Gruppe in Drupal 7

Ich versuche, programmgesteuert einen Gruppenknoten zu erstellen und dieser Gruppe einen Benutzer hinzuzufügen in Drupal 7. Der Gruppenknoten wird einwandfrei erstellt, aber der Benutzer wird nicht zur Gruppe hinzugefügt, und ich bin es Ich schätze, ich verwende die Funktion og_group falsch, bin mir aber nicht sicher. Was mache ich falsch?

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"));
}
10

Ich habe es herausgefunden. Es funktionierte schließlich nicht, da die Gruppen-ID NICHT mit der Knoten-ID für diese organische Gruppe übereinstimmt. Hier ist die Arbeitsversion:

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

Da OG7-2.x die Knoten-ID == Gruppen-ID ist, muss og_get_group () nicht verwendet werden. Und in og_group () und og_role_grant () ist Ihr Gruppentyp das erste Argument. Hier ist also der gleiche Code für 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"));

}}

13
Capono
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);
1
Suhas