I want to create a view that lists the groups user is not a member of. Tried to add the filter criteria -:(author)user:current(no),but it displays the groups user is a member & only filters the groups manage(created) by user. Views-:7.x-3.7 og-:7.x-2.3
-
what exactly u wanna go for. CAn u elaborate....– Nitesh SethiaCommented Aug 18, 2013 at 18:36
-
I want to create the view that lists the groups & user should not be a member of(non-member) any listed groups.check this node drupal.org/node/596860.– sau_tCommented Aug 18, 2013 at 18:57
2 Answers
You can create a view that lists all groups the current user is not a member of, but you need to develop your own custom Drupal module for it (if anybody knows of a simpler way, please share your knowledge).
The general idea is as follows: Create a view that lists all groups. In your custom module, implement the hook hook_views_query_alter. There, add a condition to the view's query that checks whether the current user is or isn't a member of a group.
You can create the view that lists all groups via the website, or implement it in your custom module as well. For the latter, you can get a rough idea from tutorials like this.
In the database, the memberships are stored in the table og_membership
. The field gid
refers to the group's node id in the node
table. The user who is member of the group is referred as an "Entity". So for each membership, the field entity_type
is "user", and etid
is the user id. So, a user is a member of a group if there exists a record in og_membership
with the user's id in etid
, "user" as entity_type
and the group's id as gid
. The other way around, if a user is not member in a group, such a membership record doesn't exist. In plain SQL, you could say something like (for user id 123)
SELECT n.nid, n.title
FROM node AS n
WHERE NOT EXISTS (
SELECT *
FROM og_membership AS m
WHERE n.id=m.gid AND m.entity_type = "user" AND m.etid = 123
);
In your custom module, you need to add this NOT EXISTS
-Condition to the view's query. To do that, you implement the hook hook_views_query_alter. The following is a rough idea of what the implementation should look like:
// the parameters are a reference to the view and the query, respectively
function MY_CUSTOM_MODULE_views_query_alter(&$view, &$query) {
// get the current user's info from the global scope
global $user;
// alter only the query for the correct view, not for every view on the site
if($view->name == "MY_VIEWS_NAME" && $view->current_display == "MY_CURRENT_DISPLAY") {
// the subquery to check for memberships
$member_query = "SELECT * FROM {og_membership} AS m WHERE m.entity_type = 'user' AND m.etid = :uid AND m.gid = node.id";
// add a new expression to the WHERE part of your view's query
$query->add_where_expression("AND", "NOT EXISTS (" . $member_query . ")", array(":uid" => $user->uid));
}
}
Try this in your View, using Drupal 7:
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)
-
But wouldn't that list groups the user is in, as long as any other user is that group as well? Because the filter "group membership has different user than the current user" would find those as well. Commented Apr 13, 2015 at 12:10
-
Yes, you're right. But is it possible to have an empty group? But I agree, the solution is not perfect.– FabioCommented Apr 13, 2015 at 12:38