This is either a feature request or a support issue - I apologize if it's the latter.

I would like to create a view showing only those groups of which I am not already a member (or a pending member). However, the obvious way of doing this (take the og_my view and change the filter "Organic groups: Group member" to false) does not work.

The reason is that the SQL query essentially returns one row for every user in the group that matches your conditions. If you're searching for yourself (as in og_my), each group node will only show up once; if you're not searching for yourself, each group node shows up N times, where N is the number of other group members. Thus the groups of which I'm already a member continue to be displayed as long as there's at least one other member.

Does anyone have a way around this?

Thanks, Adrian

Comments

Grayside’s picture

You may not be able to achieve this in Views directly, as it does not support subqueries. My SQL came out like so:

SELECT node.nid AS nid, node.title AS node_title FROM node node LEFT JOIN og_uid og_uid ON node.nid = og_uid.nid WHERE (node.type IN ('campaign','setting','system')) AND node.nid NOT IN (select nid from og_uid where uid = 1);

It is said you can programmatically forward the results of such a query into the Views system for theming.

Maybe a special handling of the argument Organic Groups:Member of a Group, Exclude Argument option?

Firetracker’s picture

Hi,

Did you find a solution for this?

I'm struggling

Cheers
Zap

jippie1948’s picture

Hi,

the same problem. WHERE (og_uid.uid <> ***CURRENT_USER***) doesn't seem to work.

Thanks,

JP

PS. I would like to have a panel page where the user can see in the left column of which groups s/he is member and in the right column the groups s/he potentially can join.

niklp’s picture

Had a similar (same?) issue with OG - wanted to create a view of "users that can be selected to join this group" (preferably ones that aren't already!) I can easily make a list of existing users of the group by passing the group nid into the "Provide default argument" bit of "Organic groups: Group node" argument.

Setting the argument to exclude however does not invert the query as expected.

Sarenc’s picture

subscribing

tazus’s picture

+1

johnhanley’s picture

It's hard to believe this issue hasn't been solved long ago, but I find myself struggling with the same thing. That is, how to display a list of group nodes in Views that the current user is NOT a member of.

I created two blocks: My Groups and Join Groups. Filters are set as "Organic groups: Group member True" and "Organic groups: Group member False" respectively. The former works as expected (displaying a list of group nodes the current user is a member of), but the latter shows the same list (instead of group nodes the current user is NOT a member of).

The queries for each block look correct including the inclusion/exclusion in the WHERE clause of the current user.

The solution is probably something simple and obvious, the answer alludes me at the moment. Perhaps some sleep would help. :-)

Anyway, all comments/suggestions welcome.

johnhanley’s picture

Update: the View actually works as expected when previewing the blocks in edit mode, but not when added to a panel. I wonder if it's a context issue... (?)

johnhanley’s picture

Continuing this discussion.

I have a Views block with the following fields to produce a list of groups the current user is NOT a member of:

Node: Title Project
Organic groups: Group: Description Description
Organic groups: Group: Join link

This does in fact work (sort of) as expected (i.e. lists the groups the current user is NOT a member of), but the list ALSO includes groups the user is already a member of. What's interesting is the "Join Link" (i.e. "Request membership") does NOT appear for groups the user is already a member of.

The filters for this View are as follows:

Node: Published Yes
Organic groups: Group types = Project
Organic groups: Group member False

The query output is:

SELECT DISTINCT(node.nid) AS nid,
   node.title AS node_title,
   og.og_description AS og_og_description,
   og.og_selective AS og_og_selective
 FROM node node 
 LEFT JOIN og_uid og_uid ON node.nid = og_uid.nid
 LEFT JOIN og og ON node.nid = og.nid
 WHERE (node.status <> 0) AND (node.type in ('project')) AND (og_uid.uid <> ***CURRENT_USER***)
 GROUP BY nid
  ORDER BY node_title ASC

I'm about ready to take drastic measures and either create my own custom block to output the correct groups OR modify the corresponding Views template to suppress the undesired fields. However, I'd prefer to get this working for obvious reasons. Plus, I'm sure identifying a solution would be helpful to others. (Sidebar: a quick Google search will reveal others who have been stymied by this same issue.)

johnhanley’s picture

Here's the exciting conclusion to my story.

The filter "Organic groups: Group member False" doesn't work and returns inflated results.

In my case the solution was to replace the third WHERE clause with a sub-query using hook_views_query_alter() in a custom module.

Old clause:

og_uid.uid <> ***CURRENT_USER***

New clause:

node.nid NOT IN (SELECT nid FROM og_uid WHERE uid = ***CURRENT_USER***)

Here's the full code:

/**
 * Implementation of hook_views_query_alter().
 */
function custom_views_query_alter(&$view, &$query) {
  if ($view->name == 'projects' && isset($query->fields['og_og_selective'])) {
    $query->where[0]['clauses'][2] = 'node.nid NOT IN (SELECT nid FROM og_uid WHERE uid = ***CURRENT_USER***)';
  }
}

The field 'og_og_selective' (which produces the "Request membership" link) is used to distinguish the query for the particular Views display.

I suspect this issue category should be changed from feature request to bug report, but I'll let the module maintainer decide that.

In the meantime perhaps someone will find the above workaround useful.

SophieG’s picture

hi
i am trying to do same thing in my view, i have exactly the same filters... but the og_uid.uid = ***CURRENT_USER*** does not seem to have any effect on my view...
so i am using your code, but nothing seems to work either....

Any idea why ?

Thanks !!

johnhanley’s picture

@SophieG,

My solution (workaround) works as describe. Be sure to change the view name accordingly (reads 'projects' in the above sample code).

John

SophieG’s picture

Thanks John,

I have found my mistake : my groups are private...
Do you know if i could make it work with private groups ?

Thanks!

johnhanley’s picture

I believe the above won't work with private groups because they are invitation only and are completely inaccessible to non-members.

Grayside’s picture

Right, you can never see the private groups. An administrator-oriented version that took a user as an argument could display that for someone who did have access.

SophieG’s picture

Could you explain what you mean exactly please ?

johnhanley’s picture

@SophieG,

You might want to use public groups instead of private, but limit what non-members can see. That way the above "Join" list will work as described.

John

steeph’s picture

There is another way doing this with views without writing any code. I wanted to have a block which shows all groups of which the current user is not a member and for each group a link to join it. So I added the fields for the node title and the join link. the join link is empty for the groups the user is already a member of. So I chose the title field to be excluded from display and rewrote the output of the join link field with "[subscribe] [title]". If you now chose "Hide if empty" for the join ink field, also the group title will only be shown if there is a join link for it, which is only the case if the user is not a member of the group. Anyway this will not look good for the HTML List or Table style but if Unformatted is OK for your use case, this might be a work around.

johnhanley’s picture

steeph,

I haven't tried it myself, but what you describe sounds like a viable workaround and perhaps better than my approach. Thanks for sharing.

John

dafeder’s picture

SophieG - I was having a similar problem and seem to have gotten around it using the solution here:

http://stellapower.net/blog/creating-node-view-which-bypasses-access-res...

dafeder’s picture

Another thing - if you are trying to view group posts instead of actual group nodes, it will need to be:

    $query->where[0]['clauses'][2] = 'og_ancestry.group_nid NOT IN (SELECT nid FROM og_uid WHERE uid = ***CURRENT_USER***)';
DanielWashbrook’s picture

To avoid writing any module code, there is a filter in views called 'Global:PHP' which gives three boxes. The first: SQL FILTER CODE: can return the filter we need

return 'node.nid NOT IN (SELECT nid FROM og_uid WHERE uid = ***CURRENT_USER***)';
fadgadget’s picture

Hi ive created a custom module (as per a custom module i made before) but it still seems to return all nodes. Is there anything i should be adding in the Views filter? My filters are-

Node: Published Yes
Node: Type in Forum Post

My module code is -

<?php
/**
 * Implementation of hook_views_query_alter().
 */
function custom_views_query_alter(&$view, &$query) {
  if ($view->name == 'ogfalse' && isset($query->fields['og_og_selective'])) {
    $query->where[0]['clauses'][2] = 'og_ancestry.group_nid NOT IN (SELECT nid FROM og_uid WHERE uid = ***CURRENT_USER***)';
  }
}

where ogfalse is my view (block view)

all help appreciated thanks. I would look at the filter sugggestion below but im using views 2 and not 3. thanks

vonn.new’s picture

Issue summary: View changes

#22 was the best solution for me. I had to install Views PHP first in order to get the Global:PHP filter made available.

fabioknoedt’s picture

For someone using Drupal 7, I could make it happen like this:

RELATIONSHIPS
OG membership: OG membership from Node group
(OG membership from node group) OG membership: User from OG membership

FILTER CRITERIA
(user from OG membership) User: Current (No)

shiraz dindar’s picture

In D7, I couldn't get #25 to work. #22 doesn't work because the Views PHP filter no longer provides an SQL filter box, and because the OG tables are different in D7. #10 doesn't work because OG tables are different in D7, and view queries are structured differently.

Here's what works for me:

function {module_name}_views_query_alter(&$view, &$query) {
  if($view->name == '{view_name}') {
    if ($view->current_display == '{view_display') {
      $query->add_where_expression (2, 'node.nid NOT IN (SELECT gid FROM og_membership WHERE etid = ***CURRENT_USER***)'
      );
    }
  }
}

The only thing I think that would need to be different for others is the 2 in the add_where_expression. dpm ($query->where) and add one to the highest index and use that.

claudiu.cristea’s picture

Status: Active » Closed (outdated)

This version of Drupal is not supported anymore. If this is still an issue in the 8.x-1.x branch, please open a new up-to-date ticket. Closing.