0

I have an organic group called 'newsletter'. I have two content types - 'issue' and 'article' - in 'issue' (which is set to be a group content type), there is a node reference field to articles, and many articles in a single issue, using an inline entity reference form.

I want to put an issue inside an organic group, for access control. I also want the articles that the issue references inherit that group content relationship without having to explicitly set the group for every single piece of content.

The group is public, but the content is private. I don't want anyone to access group content until they go though a process and purchase access via the commerce_licence_og module.

The article (content referenced in entity reference field) isn't added to the group content, though the issue itself is. I felt that a group reference for every article is redundant, and I would rather the article inherit the issue's access and status as group content rather than have a bunch of redundant fields and is automatic, not requiring additional admin steps.

Is there an easy way to do this? Do I HAVE TO make the articles group content in the UI?

  • You could possibly do this with rules or cer maybe, you would still have to set the OG field and make it OG content but may could set it automatically instead of with og context or entity reference pre-populate. Is there a specific reason why you don't want to set the field on that piece of content? – burnsjeremy Jun 3 '15 at 1:32
  • You could always write a custom module as well to do this, when the node is accessed you could check the reference and run it through OG access and fake the same access. – burnsjeremy Jun 3 '15 at 1:34
  • Yes, I was thinking of a custom module. I want the interface to be as simple as possible - I don't want the admins to see anything superflous. However, I think you're right, and I need to add the og fields for all the content. I was wondering if anyone had done something similar. I'll post something when I have it done. Shouldn't be too difficult. – Joshua Albert Jun 3 '15 at 16:15
0

Here is the solution I've come up with.

It works, but might not be the most elegant solution. I didn't think that this would matter too much, since it's only being called on node save, but I'm open for comments and suggestions.

/**
 * Ensure that referenced nodes belong to the same organic group
 * 
 */
function MODULE_node_presave($node) {

  if ($node->type == NODE_TYPE) {

    $og = og_get_entity_groups('node', $node);
    $nids = get_referenced_nodes($node);

    foreach($nids as $nid){

      $child_node = node_load($nid);

      //Set the values
      $values = array(
        'entity_type' => 'node',
        'entity' => $child_node,
        'state' => OG_STATE_ACTIVE,
      );  

      //Add this node to the group
      $gid = array_values($og['node']);
      $result = og_group('node', $gid[0], $values);

    }    
  }
}


/**
 * Helper function - get all node reference fields of a node
 * @param $master_nid - the node id of the referencing node
 */ 
function get_referenced_nodes($master_node) {

  $fields = field_info_instances('node', $master_node->type);

  $items = array();

  foreach($fields as $field){

    if($field['widget']['module'] == 'inline_entity_form'){
      // Get the node reference items for this field
      $items[] = field_get_items('node', $master_node, $field['field_name']);

    }

  }

  // Loop through and grab the nids
  foreach ($items as $field) {
    foreach($field as $target){
      $nids[] = $target['target_id'];
    }
  }

  return $nids;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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