If you want to list Organic Group content in a View you must do a little more that just add
one reference.
The default example is available if you enable Organic Groups. The name of that View is
"OG content" (machine-name: og_nodes).
I put it here on the blog because our usecase didn't use Organic Groups' default reference
field (og_ref that is) and because we chained multiple groups (a group has content and a
group is part of a parentgroup).
2 og-reference-fields, 3 content-types
In our case we used 2 different og-reference fields and 3 content-types were involved:
Now with a View, we want to get News-content from all Teams part of a Country.
With a normal entity reference field (not OG) you need 2 relations, in our case (yes OG) we
need 4.
Contextual filter: (if this is supposed to be on the group content's node page.)
Node ID - default value: Content ID from URL (do not use relationship)
Relationships:
OG membership: OG membership from Content
OG membership: Group Content from OG membership (link first relationship)
Fields:
If referring to group, link second relationship (called 'Group node from OG membership' by
default)
------------------------------------------
Organic Groups is a great module, but it ain't half confusing, especially when it comes to
using Views' relationships. What I wanted to do was get a list of groups that had no users. I
had to step back and understand a lot first, so am explaining it here for future reference and
for others.
The og_membership table then has an etid (entity id) and entity_type (in our case, this will
either be node or user).
Now what are all these Views relationships?
OG membership: Group Node from OG membership
This one assumes the node you started with is a group content node, because it joins it to
og_membership.etid and then goes on to join the group node.
So great for getting the group node(s) that relate to particular group content.
This one is useful if you have users defining a group. So this does not fit out typical case.
Like the previous one, it assumes you're starting with a content node and you want the group,
but for cases where the group is a user.
Again, this one assumes you're starting with group content, and it looks for other content in
the same group(s).
This one is more raw because it just joins the membership record itself again assuming that
you're starting with group content. Membership records are usually just behind-the-scenes
ways to get to related content, but one gives you access to them directly.
Here it assumes you start with the group node, not group content. As the last one, this
simply brings in the og_membership rows.
Assumes you start with group content, and gives you access to all the OG-specific user roles
for all users related to the same group(s) as the content.
OG membership: Type
Finally, this one starts with group content and finds users related to the same group(s).
However, you can't do this in Views, because the JOIN conditions are set-up by the
relationships; you can only affect the WHERE and the HAVING conditions.
Also, in looking at the tables I found that the Admin user (uid 1) had somehow been given
membership to all the groups (which were created programmatically). I also found that OG
will not let you delete this user from the group - you can try in the groups interface and it says
"Performed Remove from group on 1 item." in a green box, but it hasn't actually done
anything!
So this moved the goal posts. Again, in SQL I would just do:
SELECT g.title
FROM node g
LEFT JOIN og_membership m
ON g.nid = m.gid
AND m.group_type = 'node'
AND m.entity_type = 'user'
AND m.etid > 1
WHERE g.type = 'my_group_node_type'
AND m.etid IS NULL;
... but Views won't let me. So I resorted to a far less efficient HAVING COUNT solution as
follows
1. First, my View begins with nodes, with a node type filter for my group bundle.
2. I need the OG membership from Node group relationship, which will give me
access to all the membership records for this group.
3. I need to filter this relationship on Entity_type = user. This means I now have the
groups and their users (i.e. I've excluded the groups content nodes from the
relationships).
4. I need Use aggregation turned on.
5. I need to add another filter on the OG membership from node group relationship,
this time on Entity Id = 1, with aggregation set to Maximum.
So what's that done? Well it's looking at the maximum user Id for a group. As Admin will
always have user id 1, this works to find groups whose only user member is admin.
Phew, that was complicated. Well, if you know a better way, please leave me a comment.