I posted this in OG issues, but I really need to get a response as I'm on a time critical project.

I'm trying to insert a node into my database and assign it to an OG group programmatically. This is the code I'm attempting to use, but which does not work..

  $title = 'Test Title';
  $body = 'Test Body';
  $gid = 323;
  $group = node_load($gid);
  $groupName = $group->title;

  $node = new stdClass();
  $node = array();
  $node['type'] = 'brix';
  $node['uid'] = 1;
  $node['title'] = $title;
  $node['body'] = $body;
  $node['og_groups'][0] = $gid;
  $node['og_groups_names'][0] = $groupName;

  if ($node = node_submit($node)) {
    node_save($node);
  }

The node is inserted with all data except group info. How do I accomplish this?

Comments

SomebodySysop’s picture

Knocked around with this for a few days. Apparently, I needed a couple lines after node_save to add the node info to the og_ancestry table:

  $title = 'Test Title';
  $body = 'Test Body';
  $gid = 323;
  $group = node_load($gid);
  $groupName = $group->title;

  $node = new stdClass();
  $node = array();
  $node['type'] = 'brix';
  $node['uid'] = 1;
  $node['title'] = $title;
  $node['body'] = $body;
  $node['og_groups'][0] = $gid;
  $node['og_groups_names'][0] = $groupName;

  if ($node = node_submit($node)) {
    node_save($node);
    $sql = "INSERT INTO {og_ancestry} (nid, group_nid, is_public) VALUES (%d, %d, 0)";
    db_query($sql,$node->nid, $gid);
  }

This works.

EgbertB’s picture

Thanx for taking the trouble to post this snippet. I am building a default group posts module to give a webmaster the opportunity to create a default group with it's own set of posts (of all kinds of cck contentypes). Creating a new group then simply clones all groupposts of this default group and assigns them to the newly created group.
Got the thing working now.

Egbert - webdevelopment and implementation - www.overboord.nl

go7’s picture

Hi, I am looking for a way to create a default group with subgroups and different permissions for subgroups. Would it be possible to create groups like this with the module you are working on? I tried the skeleton module, but that saves group posts as book pages. Also will you make your module available to other Drupal users?

EgbertB’s picture

No, you cannot use it to create subgroups, although I think you could extend it. Never looked into subgroups. (Just started using OG as from end of may)
The module itself is working but is more a larve than a fullgrown module. It takes some nodes, which are wiki group posts and copies them and changes some of the group related settings.
It does what it needs to do in a very specific niche.
I do not have time to change it into something worthy of sharing.

You can have the code though ifyou want to play with it? No warranties and sorry for the mess....

// $Id$

// http://api.drupal.org/api/file/developer/examples/node_example.module/6
// http://api.drupal.org/api/function/node_example_insert/6
// http://drupal.org/node/239895
// http://drupal.org/node/255005


/**
 * Implementation of hook_nodeapi().
 */
function zzln_og_defaultpages_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert':
	if ($node->type=='werkplek') {// when a new werkplek (werkplek is a group node) is being created, clone the default nodes and attach them to this new group
          // here I should query the nodes attached to the default group and then clone them, but I just list them
          // another feature would be to define the default group in the admin section...
         // then clone this group with all of its subgroups and all of its posts
	  zzln_og_copythis($node->nid,$node->title,13);
	  zzln_og_copythis($node->nid,$node->title,11);
	  zzln_og_copythis($node->nid,$node->title,10);
	  zzln_og_copythis($node->nid,$node->title,8);
	  zzln_og_copythis($node->nid,$node->title,316);
   }
   break;
  }
}// function zzln_og_defaultpages_nodeapi

// http://www.k4ml.com/node/246
//http://www.nabble.com/clone-a-node-td18565428.html

/**
* Display help and module information
* @param path which path of the site we're displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
*/
function zzln_og_defaultpages_help($path, $arg) {
  $output = '';  //declare your output variable
  switch ($path) {
    case "admin/help#zzln_og_defaultpages":
      $output = '<p>'.  t("Copies a predefined set of nodes ands adds them to a newly created group") .'</p>';
      break;
  }
  return $output;
} // function  zzln_og_defaultpages_help



/**
* Display help and module information
* @param path which path of the site we're displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
*/
function zzln_og_copythis($groupid,$grouptitle, $copynodeid) {
  global $user;
	  $clone=node_load($copynodeid);
	  // tell drupal it is a new one
      $clone->is_new = true;
	  // let it be owned by the current user
	  unset($clone->uid);
      $clone->uid = $user->uid;
	      watchdog('zzln_og_defaultpages', 'og clone uid '.$user->uid, null, WATCHDOG_ERROR);	
 	  // unset node en version id's
      unset($clone->nid);
      unset($clone->vid);
	  unset($clone->path);
      // different handling of the schoolportret contenttype
	  if ($clone->type === 'schoolportret') {
          unset($clone->title);
		  $clone->path = 'schoolportretten/'.$grouptitle;
	      $clone->title=$grouptitle;
	  } else {
        $clone->path = 'scholen/'.$grouptitle.'/'.$clone->title;
	  }
      $clone->og_groups= array('0'=>$groupid);
      $clone->og_groups_both= array($groupid => $grouptitle);
     // Group id is node id of group
     // http://drupal.org/node/348078
	 // node->og_groups is array with all groupode ids (nid's van de group, waar de node onder sorteert)
	 // so what is the function of  og_groups_both array??? Contains group name...

     // Still both the group connection and the nodepath are not saved to the clone.... Why is that?
     // User is also not transported to the clone (But as the default group is owned by the webmaster in this case this does not worry me)
     // so need to do this
     if ($clone = node_submit($clone)) {
       node_save($clone);
       $sql = "INSERT INTO {og_ancestry} (nid, group_nid, is_public) VALUES (%d, %d, 0)";
       db_query($sql,$clone->nid,$groupid);
	    $sql="UPDATE {node_access} set `gid` = ".$groupid." where `nid` =". $clone->nid."";
	    db_query($sql);
	  }
	
     //http://drupal.org/node/410286
    path_nodeapi($clone, 'insert', NULL);	
} // function  zzln_og_copythis

Egbert - webdevelopment and implementation - www.overboord.nl

liquidcms’s picture

For D6 i did this in a nodeapi $op = insert when creating a group node to automatically create a wiki node within my new group:

/**
 * Implementation of hook_nodeapi().
 */
function mymodule_nodeapi(&$node, $op, $arg, $page) {
  
  switch ($op) {
    
    case 'insert':
          
      // let's autocreate a Wiki node for our Top wiki page when we create a Group
      if ($node->type == "group") {
        global $user;
        include_once (drupal_get_path('module', 'pathauto') .'/pathauto.inc');
        $path = pathauto_cleanstring($node->title);
         
        $wiki->type = "wiki";
        $wiki->status = 1;
        $wiki->uid = $user->uid;
        $wiki->name = $user->name;
        
        $wiki->title = $node->title . " Wiki";
        $wiki->body = t("This is the Wiki for your Community. Any member is allowed to edit this page. Be the first to edit this page by selecting the EDIT tab above and deleting this content and replacing it with your own.");
        $wiki->path = "wiki/" . $path;
        $wiki->og_initial_groups[0][$node->nid];
        $wiki->og_groups[0] = $node->nid;
        $wiki->og_public = 0;
        $wiki->og_groups_names[0] = $node->title;

        if ($wiki = node_submit($wiki)) {
          node_save($wiki);
          $sql = "INSERT INTO {og_ancestry} (nid, group_nid) VALUES (%d, %d)";
          db_query($sql, $wiki->nid, $node->nid);
        }
       
      }
      
      break;    
  }

}
thelionkingraja’s picture

hello,

I used your code, everything works fine and the path alias creating 2 times.

One is normal from alias settings and one from code.

Please tell me how can i stop the automatic path alias two avoid conflicts.

Thanks in advance

liquidcms’s picture

try commenting out this line:

$wiki->path = "wiki/" . $path;

thelionkingraja’s picture

Hi there,

I am trying to use the path from the code. Not the automated alias path. Please help me how to achieve this ?

Thanks in Advance.

irkop’s picture

I'm also creating wiki nodes to og pretty much similar way, but I'm having problems with wiki node's access rights. When user subscribes to group they don't see edit buttons for the wiki nodes until author saves the node again. I found out that node_access table had gid value 0 for those nodes when created. I think it has something to do with the og_access module and granting rights but I couldn't figure it out. Any help would be appreciated.