grouping nodes with collapsible fieldset I use entityreference to link nodes to other nodes and field_group to display collapsible fieldsets with fields.

Assume I have node#1 which has a field related_nodes containing node#2, node#3 and node#4.

The display of this node looks like this:

node title

node body

related: 
- node#2
- node#3
- node#4


I can use hook_node_view to remove nodes from the related_nodes field on the fly using something like:

unset($node-&gt;content['field_info_case_law_references']['#items'][?]);


I want to remove all those nodes (e.g based on their title) and place them in groups, each in a collapsible fieldset within that related: field. so it ends up looking something like this:

node title

node body

related:
+ starting with A (collapsible fieldset)
  - node#2
+ starting with B (collapsible fieldset)
  - node#3
  - node#4


The default collapsible behaviour, html and styling of that I can configure from admin/structure/types/manage/*/display is good enough.

Where should I look at?
OK I found it.

In the HOOK_node_view I can do:

if ($node-&gt;type=='CONTENT_TYPE') {

  // clone from a fieldset nearby
  $node-&gt;content['#fieldgroups']['group_FOO']=clone $node-&gt;content['#fieldgroups']['group_ORIG'];

  // modify fieldset
  $node-&gt;content['#fieldgroups']['group_FOO']-&gt;label="xxxxxxxxxxxxxxxxxxxxxxxx";
  $node-&gt;content['#fieldgroups']['group_FOO']-&gt;weight=-1;
  $node-&gt;content['#fieldgroups']['group_FOO']-&gt;identifier=...
  $node-&gt;content['#fieldgroups']['group_FOO']-&gt;group_name=...

  // clone from a field nearby
  $node-&gt;content['field_FOO'] = $node-&gt;content['field_ORIG'];

  // modify field
  unset($node-&gt;content['field_FOO']['#items'][0]);
  unset($node-&gt;content['field_FOO']['#items'][1]);
  unset($node-&gt;content['field_FOO']['#items'][2]);

  // position fieldset and field
  $node-&gt;content['#group_children']['group_FOO'] = 'group_ORIG';
  $node-&gt;content['#group_children']['field_FOO'] = 'group_FOO';

}