I'm experimenting with a sub-module for Organic Groups that adds some sample content to help people get started with the module. 

I'm having a hard time with the code to create an entity reference.

I have first created some groups and some group nodes. I'm now trying to create some group content in a group_content content type, but I'm struggling with how to create the link between the group content node and the group that owns it.

Any help would be appreciated or links to helpful resources.

UPDATE:

Initially, I thought I was just trying to create an entity reference. But, I think it's more specific than that. I'm finding a number of references to this fuction - "og_group()" - to add a og membership. This might be key to my answer.

Comments

My difficulties were not so much around entity references or the specifics of creating content nodes for Organic Groups. I was dealing with another unresolved conflict between creating content types and content in the same module (using config files to generate the content types and fields). 

See: https://github.com/backdrop-contrib/og/issues/102

Here is my code that is working, as long as it's in a separate sub-module from the config files creating the content types. 

Specifically see part about "og_group_ref" for the entity reference. I had to get the NID for each group in order to make this connection.

$groups = array(
  'Red' => array(
    'title' => st('Red Group'),
    'body' => st('<p>This is a group for folks who love red things.</p>'),
  ),
  'Green' => array(
    'title' => st('Green Group'),
    'body' => st('<p>This is a group for folks who love green things.</p>'),
  ),
  'Blue' => array(
    'title' => st('Blue Group'),
    'body' => st('<p>This is a group for folks who love blue things.</p>'),
  ),

);

foreach ($groups as $group => $info) {
  $group = new Node(
    array(
      'title' => $info['title'],
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => $info['body'],
            'format' => 'filtered_html',
          ),
        ),
      ),
      'uid' => 1,
      'status' => 1,
      'promote' => 0,
      'type' => 'group',
    )
  );

  $group->save();

}

$group_content = array(
  'Blood' => array(
    'title' => st('Blood'),
    'body' => st('<p>They say that blood is thicker than water. It is also red and belongs in the red group.</p><p>The blood moon prophecies were a series of prophecies by Christian preachers John Hagee and Mark Biltz, related to a series of four full moons in 2014 and 2015. The prophecies stated that a tetrad (a series of four consecutive lunar eclipses—all total and coinciding on Jewish holidays—with six full moons in between, and no intervening partial lunar eclipses) which began with the April 2014 lunar eclipse was the beginning of the end times as described in the Bible in the Book of Joel, Acts 2:20, and Revelation 6:12. The tetrad ended with the lunar eclipse on September 27–28, 2015.</p>'),
    'group' => st('Red Group'),
  ),
  'Shamrocks' => array(
    'title' => st('Shamrocks'),
    'body' => st('<p>The luck of the Irish is green.</p><p>A shamrock is a young sprig, used as a symbol of Ireland. Saint Patrick, Ireland patron saint, is said to have used it as a metaphor for the Christian Holy Trinity. The name shamrock comes from Irish seamróg ([ˈʃamˠɾˠoːɡ]), which is the diminutive of the Irish word seamair and simply means young clover.</p>'),
    'group' => st('Green Group'),
  ),
  'Sky' => array(
    'title' => st('Sky'),
    'body' => st('<p>The sky is blue and the clouds are gray. Yes, I can see for miles and miles.</p><p>Blue is one of the three primary colours in the RYB colour model (traditional color theory), as well as in the RGB (additive) colour model. It lies between violet and cyan on the spectrum of visible light. The eye perceives blue when observing light with a dominant wavelength between approximately 450 and 495 nanometres. Most blues contain a slight mixture of other colours; azure contains some green, while ultramarine contains some violet. The clear daytime sky and the deep sea appear blue because of an optical effect known as Rayleigh scattering. An optical effect called Tyndall effect explains blue eyes. Distant objects appear more blue because of another optical effect called aerial perspective.</p>'),
    'group' => st('Blue Group'),
  ),
  'Water' => array(
    'title' => st('Water'),
    'body' => st('<p>The sea is also blue, except in the middle of garbage island.</p><p>The Great Pacific garbage patch (also Pacific trash vortex) is a garbage patch, a gyre of marine debris particles, in the central North Pacific Ocean. It is located roughly from 135°W to 155°W and 35°N to 42°N.[1] The collection of plastic and floating trash originates from the Pacific Rim, including countries in Asia, North America, and South America.[2] The gyre is divided into two areas, the "Eastern Garbage Patch" from California to Hawaii, and the "Western Garbage Patch" extending from Hawaii to Japan.</p>'),
    'group' => st('Blue Group'),
  ),

  );
  foreach ($group_content as $content => $info) {
    og_demo_content_get_title($info);
    $content = new Node(
     array(
      'title' => $info['title'],
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => $info['body'],
            'format' => 'filtered_html',
          ),
        ),
      ),
     'og_group_ref' => array(
        LANGUAGE_NONE => array(
          array(
            'target_id' => $info['group_id'],
          ),
       ),
     ),
    'uid' => 1,
    'status' => 1,
    'promote' => 0,
    'type' => 'group_content',
    )
  );
  node_save($content);
  }
}

function og_demo_content_get_title(&$temp) {

  // Gets a list of all groups that are nodes
  $mygroups = og_get_all_group('node');

  // Finds group that has the same title
  foreach ($mygroups as $gid) {
    $group_title = node_load($gid)->title;
    if ($group_title == $temp['group']) {
      $temp['group_id'] = $gid;
    }
  }
  return $temp;
}