Listing an Organic Group's Content in a Block using Views

I'm new to Organic Groups (OG). If you are too, then it's a way to relate group content together, e.g. you could have a set of campaigns, and each campaign may have a number of petitions, blog posts, users etc.

Goal: I would like my campaign (i.e. group) pages to list the petitions that are part of that campaign (i.e. group content) in a Block, using Views.

Reading around, I've found a heavy emphasis on using Panels in order to deliver this. As I'm a fan of Display Suite (DS) which is not compatible with Panels, I did not really want to have to start from scratch with Panels. Also I find Drupal very resource-intensive anyway and Panels being heavier than DS was another reason for me to find another way.

I also discovered the Entity Views Attachment project which basically allows a view of related content to be displayed as a field. This has the ability to filter on a token. However, I wanted my output in a block to give more flexibility of placement. Also, DS provides the functionality to use a block (and therefore a view) as a field, so this would be duplicating functionality, except for the token filtering.

OG provides a display-less View for cloning/editing that looks like it would do the job, but it has no way to filter content (petitions in our example) based on the group (campaign), so no good for our task.

Understanding the task

OG groups content by adding extra tables.  It's important to understand these in order to understand the otherwise cryptically named relationships and contextual filters that OG provides.

Group content is linked to a group by the og_membership table. This table will tell you the OG id (gid). The gid is found in the og table which links to the group itself.

group content (e.g. petition nodes) → og_membershipog → group (e.g. the campaign node)

Creating the View

We want a view listing all petitions for the campaign that the user is viewing. So we set up a View with a Block display for petition nodes. Then use some relationships and then a contextual filter to filter only for the petitions that belong to that campaign.

  1. Set up a View with a Block display for petition nodes in the normal way.
  2. Now add the OG membership from node relationship. Tick the Required relationship box. This brings in the og_membership table.
  3. Next we add another relationship: OG group gid. Again, tick Required. This builds on the first relationship and brings the og table in.

    We could go as far as adding in the campaign node itself, but we don't need to. The  og table contains the nid of the campaign page, which is all we need for the final step. Note that the og table knows of entity ids not nids as OG groups can be made from any entities, not just nodes.
     
  4. Add a contextual filter on OG group: Entity Id. Choose Provide default value and Content ID from URL. This says that the OG entity Id (i.e. the campaign node id related to a petition) must be that of the campaign page being viewed.

Save the view and put the view block on campaign pages and you're done.

 

 

 

Tags: 
Pull Out: 

Thanks to Karen Stevens over at Lullabot for her helpful intro to OG.

Add new comment