Question

How to assign an entity to a group programmatically? I have referred to this question however it doesn't seems to work:

$values = array(
    'type' => 'my_type',
    'uid' => $user->uid,
    'status' => 1,
    'comment' => 1,
    'promote' => 0,
);
$entity = entity_create('node', $values);
$ewrapper = entity_metadata_wrapper('node', $entity);
$ewrapper->body->set(array('value' => $form_state['values']['body']));
$ewrapper->save();
$result = og_group('node', $form_state['values']['group_id'], ['entity type' => 'node', 'entity' => $ewrapper, 'state' => OG_STATE_ACTIVE]);
$ewrapper->save();
Was it helpful?

Solution

According to the API documentation, my code was missing an underscore character, and I should pass in the entity. So instead of

$result = og_group('node', $form_state['values']['group_id'], ['entity type' => 'node', 'entity' => $ewrapper, 'state' => OG_STATE_ACTIVE]);

it should be

$result = og_group('node', $form_state['values']['group_id'], ['entity_type' => 'node', 'entity' => $entity->nid, 'state' => OG_STATE_ACTIVE]);
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top