0

How can I assign a node to an organic group in code? After consulting following page

And de OG API I ended up with this code, but it throws an error 'The website encountered an unexpected error. Please try again later.'

$node = node_load(5827);
$values = array('entity type' => 'node', 'entity' => $node, 'field_name' =>'og_group_ref');
og_group('node', 3, $values);
node_save($node);

More info: OG-7.2.7

3
  • Check the watchdog, it will contain the real error message
    – Clive
    Commented Jan 21, 2015 at 11:51
  • Thanks for the advice. It seems like it want to add it to a user type instead of a node group type. OgException: OG membership can not be created in entity <em class="placeholder">user</em> and bundle <em class="placeholder">user</em> using the field <em class="placeholder">og_group_ref</em> as the field does not exist. in OgMembership->save() (line 50 I've tried the code on a clean install, but it gave me an identical error More info, but no success: drupal.org/node/1734526 drupal.org/node/1421478
    – kim.kaho
    Commented Jan 21, 2015 at 12:32
  • Ok, the issue was caused by type error in the values array. 'entity type' must be 'entity_type'. Now the code is error free but the nodes aren't assigned to group. Any suggestions?
    – kim.kaho
    Commented Jan 21, 2015 at 12:52

1 Answer 1

-1

Found out removing the last line from the snippet 'node_save($node)' and the above type error was the reason for my problem. Thanks for the support.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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