Hello
I'm having some trouble creating an organic group node programmatically in D7.
I have a content type of "test_node_type" which is configured as a group content post and it has a field called "my_node_group" to set the Groups audience.
I need to create nodes of this type programmatically. The code below creates the node just fine, but I can't find any guidance on how to assign the node to a group by setting the groups audience.
$values = array(
'type' => 'test_node_type',
'uid' => $user->uid,
'status' => 1,
'comment' => 1,
'promote' => 0,
);
$entity = entity_create('node', $values);
$wrapper = entity_metadata_wrapper('node', $entity);
$wrapper->title->set('My node title');
$my_body_content = '';
$wrapper->body->set(array('value' => $my_body_content));
$wrapper->body->summary->set('');
$file_path = drupal_realpath($my_file_path);
$file = (object) array(
'uid' => 1,
'uri' => $file_path,
'filemime' => file_get_mimetype($file_path),
'status' => 1,
);
$file = file_copy($file, 'public://',FILE_EXISTS_RENAME);
$wrapper->field_test_file = (array)$file;
$wrapper->save(true);
entity_save('node', $entity);Here is the code I've attempted to use to assign the node to a group (I've checked that 9 is the correct entity id for the group)
$group = array(
'und' => array(
0 => array(
'target_id' => 9,
),
),
);
$wrapper->my_node_group->set($group);Which gives the error "Warning: Illegal offset type in OgBehaviorHandler->groupAudiencegetDiff() (line 153 of ... og\plugins\entityreference\behavior\OgBehaviorHandler.class.php"
and
$wrapper->my_node_group->set(9);
which gives the error
Warning: reset() expects parameter 1 to be array, integer given in EntityListWrapper->set() (line 1026 of ...\sites\all\modules\entity\includes\entity.wrapper.inc).
EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityMetadataWrapper->set() (line 122 of ...modules\entity\includes\entity.wrapper.inc).
The thing is, I don't even know if I'm going about it the right way - is setting that og field sufficient or is anything else I need to set?
I'm sure there is a standard and proper way to do it, I've just been unable to find it, so any guidance would be much appreciated!
Comments
You need to use the og_group(
You need to use the og_group() function to add membership.