I have a view running, which shows all groups (the nodes) the user has a membership, but now I want to filter this view more specific to show only groups the user has a specific Role.

I'm using the "OG User groups", which is included inside the newest -dev. But I didn't succeeded with filtering only for the roles. I found in og.views.inc some permissions view alters, but unfortunately I'm don't have a lot of experience in creating views plugins.

I am missing something or is this functionality not yet implemented?

Thanks

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

amitaibu’s picture

Category: support » feature
Status: Active » Postponed

> but now I want to filter this view more specific to show only groups the user has a specific Role.

This doesn't exist yet. Postponed until after 7.x-1.1 is out.

Gemma Morton’s picture

Title: Group Role Relation Handler for Views » OG Roles in Views: Patch Attached
FileSize
4.18 KB

I have attached a patch which uses the og_user_roles table to help you filter by Users with Roles in a Group. It also has a Numeric Argument filter which *should* work, and a Field which instead of using field_data uses this table too. Please test and commit if it works for you. I spent well over a day figuring this out. There is probably a lot of other work that can be done to improve this integration but for the purposes of having a Filter in your View, and Field of Roles, this is sufficient.

Gemma Morton’s picture

Status: Postponed » Needs review

Changed Status to "Needs Review" as I dont want this getting lost in the Postponed Queue.

amitaibu’s picture

I think we should postponed it after 1.1 is out (which should happen in a few days , if everything goes well), as Roles the and permissions might become an entity, to allow more flexibilty, so also the Views part will change.

Gemma Morton’s picture

Status: Needs review » Postponed

Ok, that's fine. This patch doesn't work as well I had hoped anyway. It's a starting point though, for anyone who wants to take a crack at it. It's broken my brain for too long now, and I need to get going with something that works, so for now Im just going to use a "Views Query Alter" hook.

---
Changed status back to postponed

amitaibu’s picture

@mckeen.greg ,

OK, thanks for the work.

wjaspers’s picture

Status: Postponed » Needs review
FileSize
1.85 KB

Here's a simple patch that adds the "group role" data to Views.

wjaspers’s picture

Here's a p0 patch so it can be applied in drush make, too.

tehbmar’s picture

FileSize
2.07 KB

With some code from the patches

http://drupal.org/files/issues/og-user-roles.patch written by @mckeen.greg
and
http://drupal.org/files/issues/og-roles-in-views-1164516-7.p0.patch written by @wjaspers

and help from @matt2000 we wrote a module that creates a filter that allows you to filter by group roles and actually lists the roles to filter by.

Matt will be contributing a patch that will add this modules functionality to OG itself. But until then you guys get a stand alone module.

mrfelton’s picture

I was looking for OG Roles support in Views, but as an access check rather than as a filter...

matt2000’s picture

Here's the patch as promised in #9

amitaibu’s picture

Note that this experimental branch might solve it #1261376: Entity-ify Roles

fago’s picture

FileSize
4.93 KB

ok, looks like that experimental branch is deprecated.

I've tried the patch from #11 and noted incorrect results, as views does not pick up both joins listed for the table. Afaik there is no way in views to join the table from both tables, in particular when joining from another relationship.
To make it work, I've just fixed the handler to add the constraint to any (foreign) user relationship. So you can relate your membership entities somehow with users, to which the user-roles are then associated.

I tried to filter or list user roles for a og-members view, thus joining from users to og-memberships. In that scenario the existing og-roles field resultet in duplicates as the field-table was not in any use yet. The same behavior resulted when using the new og-roles field introduced by #11 . To fix that, I've moved the field to the og_membership table. However, that way just selecting user.uid via additional fields does not work, as there is no way to join back to the users table for views (it's a relationship!). Thus, I had to fix it to just make use of an existing relationship too.

Also, patch added fields for gid and uid again - what's not usual in views, so I've removed that.

Patch attached what allows me to create a view
* listing users, relating to og-memberships - thus restrict users by membership of a certain group
* show og user roles for that users + allow filtering by og user role

fago’s picture

oh, I forgot a point:

+    unset($this->value_options[OG_ANONYMOUS_ROLE]);
+    unset($this->value_options[OG_AUTHENTICATED_ROLE]);

I removed those roles from the available options as there are no DB values for them.

Status: Needs review » Needs work

The last submitted patch, og_roles.patch, failed testing.

fago’s picture

Status: Needs work » Needs review

#13: og_roles.patch queued for re-testing.

amitaibu’s picture

Status: Needs review » Needs work
FileSize
206.61 KB

I'm getting an error in og_handler_field_prerender_list, if there's no UID

fago’s picture

Status: Needs work » Needs review
FileSize
6.15 KB

ok, I worked over the patch again to ensure it works correctly for the following cases:
* base table node, relate to membership
* base table user, relate to membership

I've improved the code to just use the first users base table, ideally I think it would be configurable which relationship should be used. However 95% probably would just have a single users relationship added, so that should do it for now.
I also fixed the errors if no users base table is there, in that case just nothing is shown / it is filtering for all users (does any user have role X in that group). I added a short note that the field requires a user relationship to work.

Also, I fixed the code excluding anonymous, authenticated roles from the roles-options + removed the argument + sort for the role-ids as they'd probably require the same tweaking as the field/filter.

Updated patch.

amitaibu’s picture

Status: Needs review » Fixed

Committed, thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

justduet’s picture

I'm using 7.x-1.5 and I'm having trouble filtering by OG user roles. In 'Filter Criteria', I'm filtering by 'OG user roles: Role ID' and I've selected the specific role that I'd like to include. I've noticed that the WHERE clause in the query only includes the 'og_users_roles.rid' for group (gid) = 0. Below is the query that is generated. In my case there should be also be a rid '8' from another group that is not included. I'm not sure if I'm doing something incorrect or if this is still an issue.

SELECT og_membership.id AS id, users_og_membership.uid AS users_og_membership_uid, og_membership.gid AS og_membership_gid, og_og_membership.gid AS og_og_membership_gid, users_og_membership.name AS users_og_membership_name
FROM
{og_membership} og_membership
LEFT JOIN {og} og_og_membership ON og_membership.gid = og_og_membership.gid
LEFT JOIN {users} users_og_membership ON og_membership.etid = users_og_membership.uid
INNER JOIN {og_users_roles} og_users_roles ON og_membership.gid = og_users_roles.gid
WHERE (( (og_users_roles.uid = users_og_membership.uid) AND (users_og_membership.uid = '19' ) )AND(( (og_users_roles.rid = '4') )))
LIMIT 10 OFFSET 0

Edit: The intended purpose of the above View was to restrict content creation by OG Role in each group but I have since switched to 7.x-2.0-beta2 and this functionality now seems easier to achieve.

akalam’s picture

I cannot see the filter when I use "Entity Reference" display type, neither using User or Organic Group Membership as base table. I see it if I use a block display type

martinvs’s picture

I have the same problem as justduet in post #21, using 7.x-1.5 and having trouble filtering by OG user roles. So I also tried to upgrade from OG 7.x-1.5 to 7.x-2.0-beta3. But I can't disable the og module because of fields that use the module (group audience) and I don't want to loose this data. Justduet, can you explain how you upgraded from OG 7.x-1.5 to 7.x-2.0-beta3.

crashtest_’s picture

Version: 7.x-1.x-dev » 7.x-2.0-rc3
Status: Closed (fixed) » Needs work

I believe that this has regressed on the 2.0 version of this module. Is there any way you could look at how to get the filter handler back into this branch?

Thanks!

dsdeiz’s picture

I think it's still there. Add a relationship for "OG Roles from membership" and select "OG user roles: Role Name" in the filter.

chicodasilva’s picture

The option is there but this is still but the filter is not working as desired.

This handler also crashed with a PostGres Database.

 function get_value_options() {
    $this->value_options = array();
    // Get all role names.
    $results = db_select('og_role', 'r')
      ->fields('r')
      ->groupBy('r.name')
      ->groupBy('r.rid') // this needs to be added
      ->execute();
    foreach ($results as $result) {
      $this->value_options[$result->name] = $result->name;
    }
 
joachim’s picture

Status: Needs work » Fixed

Closing this again:

> I think it's still there. Add a relationship for "OG Roles from membership" and select "OG user roles: Role Name" in the filter.

Yup.

> This handler also crashed with a PostGres Database.

I think it would be best to file a new bug report for this, and other follow-ons.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

vistree’s picture

Issue summary: View changes

Sorry for commenting on a closed issue - but is it possible to modify the patch in a way, we can use the OG role names (not the weight!!!) for sorting?

vistree’s picture

Is it better to create a new issue instead of adding such a question to a closed one?