How to embed a view or a rendered node into a field group I'm afraid my question is a bit to general because I don't quite know where to start.  I still really don't have my head wrapped around the drupal 7 theming system.

Scenario

I have two content types:


Type main
Type secondary (this type has a required entity reference field, referencing Type main


The display on Type main has a set of 2 horizontal tabs htabs group named group_expandables.  

Desired Outcome

What I would like to happen is, if an entity of Type secondary has been added referencing this node; add a third horizontal tab containing the Type secondary node and make it the default.

Initial Approach

These are the steps I thought through that should probably happen on page load for a node of type Main:


Look for a Type secondary node referencing this node, return if not found
If found, build up a new render array for the Type secondary node htab.  I was thinking it would be easiest to build this display in views, and load in the view, but doing it in a template file would probably work too.
Once the variable has been for the new htab, insert it in the htabs  render array and modify the render array to remove the open by default indicator on the old default tab.


So far I have used hook_preprocess_page to grab the $variables['page']['content']['system_main']['nodes'][nid]['#fieldgroups']['group_expandables'] and add a new group name to its children, clone one of the htab field groups and append it to the ['#fieldgroups'] array, and modify the elements that referenced the old htab and replace them with the name for the new tab.  Once I figured out that I needed an entry in #group_children too, I actually got the tab to appear in the group, but I haven't had any luck getting content to show up in it yet.

Question

Here are the places I'm really hung up:


Is hook_preprocess_page the best place to start this?
Is my general strategy for how to go about this sound?
How would I build my desired content into the new htab?


I'm sorry for the broad nature of the question, any pointers would be greatly appreciated.
I'm not sure your general strategy is the absolute best way to go, but I don't see any major problems, I think hook_preprocess_page is too broad as it runs everywhere and would suggest template_preprocess_node instead (even though this is a template preprocess it can be run like a normal hook preprocess in a module)

In regards to getting the content you will need to do a EntityFieldQuery using the nid of the page being viewed as a fieldCondition, the only problem I can see is when there is more than one secondary type assigned to the same main type.

An alternative approach would be to see whether it's possible to write a View which takes the nid as a Contextual Filter and then builds all the content, I think you'd need to add a Relatonship to the secondary type, etc. and if it's possible this would cut down on the amount of custom coding needed.