Is there any way to have "Role" filter for views? I've looked trough the group's relationships but haven't found anything which allows to filter members by specific roles such as member, moderator, admin, etc.

Thanks in advance.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nedjo’s picture

Are you using the 7.x-1.x dev branch? You'll need it for the roles views exposure implemented in #1164516: OG Roles in Views: Patch Attached.

creando sensaciones’s picture

I'm using OG 7.x-2.0 alpha 2. Is there a way to filter by role? I need to make views showing all members of a certain Group role.

Thanks

Pomliane’s picture

Version: 7.x-1.3 » 7.x-2.x-dev
Priority: Normal » Major

It seems OG roles in Views have not been fully implemented in og-7.x-2.x yet—or I'm missing something.
As they appear to be implemented in 7.x-1.x, isn't it a regression/bug?
At least like a major feature, doesn't it?

Sylense’s picture

subscribe

creando sensaciones’s picture

As I didn't found a solution I made myself a little function to extract them directly from the database:

Returns an array with uid, name and picture-uri of all group members with a certain role.
needs Group id and Role id

function my_module_og_get_member_by_role($gid, $rid) {
$query = db_select('og_users_roles', 'o');
$query->fields('o', array('uid'));
$u_alias = $query->innerJoin('users' ,'u', '%alias.uid = o.uid');
$f_alias = $query->leftJoin('file_managed' ,'f', '%alias.fid = u.picture');
$query->addField($u_alias, 'name');
$query->addField($f_alias, 'uri');
$query->condition('o.gid', $gid);
$query->condition('o.rid', $rid);
$result = $query->execute();
return $result; 
}
SebCorbin’s picture

Issue tags: +Localize D7 port

Tagging

Chipie’s picture

How can I use this function in a view?

Renee S’s picture

Category: support » feature
Renee S’s picture

Component: og.module » Og Views
SebCorbin’s picture

Status: Active » Needs review
FileSize
3.23 KB

This patch is a test, just throwing it here for testbot

Status: Needs review » Needs work

The last submitted patch, 1510384-og_roles-views.patch, failed testing.

ifish’s picture

Is there still no the most basic work around for this? Thanks in advance..

ifish’s picture

If possible then a very basic Views PHP Code filter would be awesome for a fast fix. It's possible to show the current user "roles" in the fields section, so I'm wondering if it's possible to use this in the Filter criteria, such as if user has this role, then show.

kaw3939’s picture

Confirmed - Same problem

SebCorbin’s picture

Status: Needs work » Needs review
FileSize
3.68 KB

Walkthrough:

  • Add the OG membership relationship
  • Add the OG roles from membership relationship
  • Add the filter "OG user roles: Role Name", filtering is done on role name, so it should work for global roles and per-group roles

Comments highly welcome

ifish’s picture

FileSize
165.7 KB

I'm not sure if I've done something wrong but I'm getting broken handler error

SebCorbin’s picture

YOu need to flush your caches for the handler to be discovered in og.info

ifish’s picture

Works great, thanks alot for this! However, it seems like it has removed the "user role field" that was used by OG before, it's now displaying it as an broken handler. Unless I'm doing something wrong.

SebCorbin’s picture

I still have the field "(OG membership from user) OG membership: OG user roles in group" on my end, I left the code providing that field as it was, though the new relationship could be harnessed

ifish’s picture

Ah for some reason "class og_handler_field_user_roles extends og_handler_field_prerender_list { " etc was removed.

Also on a side note, I'm not quite sure if it's actually the user role's handler problem but if selecting Operator "Is none of" it doesn't seem to do anything.

amitaibu’s picture

Status: Needs review » Needs work

Thanks for working on this.

+++ b/includes/views/handlers/og_handler_filter_user_roles.incundefined
@@ -0,0 +1,41 @@
+  function init(&$view, &$options) {
+    parent::init($view, $options);
+  }

Leftovers?

+++ b/includes/views/handlers/og_handler_filter_user_roles.incundefined
@@ -0,0 +1,41 @@
+    // Get a user table we can relate to.

user table?

+++ b/includes/views/og.views.incundefined
@@ -56,6 +54,39 @@ class OgMembershipViewsController extends EntityDefaultViewsController {
+      // Provide a possible relation ship to roles through uid

relationship. Also missing dot.

+++ b/includes/views/og.views.incundefined
@@ -56,6 +54,39 @@ class OgMembershipViewsController extends EntityDefaultViewsController {
+        // FIXME: Will not work for relations other than of user type

Can you explain?

SebCorbin’s picture

Status: Needs work » Needs review
FileSize
3.41 KB

The FIXME was in fact irrelevant as we cannot have memberships other than related to user entity (I guess?)

As for the "Is none of", I tried it and it works, so if you can provide me you views structure and possibly a query, that'd help me a lot.

New patch attached as per #21 (simplified the class).

amitaibu’s picture

> The FIXME was in fact irrelevant as we cannot have memberships other than related to user entity (I guess?)

No, the og-membership can be related to any entity. It treats users and nodes the same way.

SebCorbin’s picture

Well, considering the relationship with user already adds a condition on entity_type, this should not be a problem

INNER JOIN {og_membership} og_membership_users ON users.uid = og_membership_users.etid AND og_membership_users.entity_type = 'user'

amitaibu’s picture

@SebCorbin,
I'm unable to see this filter -- can you attach an exported View so I can see it working?

SebCorbin’s picture

@Amitabiu the second exported in this file (http://drupalcode.org/project/l10n_server.git/blob/73fc0cdeffec993c30b51...) makes use of OG Roles filter

bkildow’s picture

On my initial test, #22 appears to work for me. I was able to create a new group role, assign some people to it in a group, then display only those people assigned to this role following the walkthrough in #15. Thank you for your efforts!

@Amitaibu, I didn't see this filter at first either. It appears under a different filter grouping: "OG user roles". I was looking under "OG membership" in the filter criteria screen, and I didn't see it. Searching for "role" in this screen also showed it.

amitaibu’s picture

@SebCorbin, @bkildow
Sorry I'm unable to see it working. Can you please create a simple View based on OG-example content types, and write the steps to reproduce?

SebCorbin’s picture

  • Downloaded and patched og (patch applies to 7.x-2.0-rc4 as well as 7.x-2.x-dev)
  • Installed og_example and og_context
  • created a group
  • created 3 users: one admin of the group, one member of the group, one non-member
  • created the following view
$view = new view();
$view->name = 'group_admins';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'users';
$view->human_name = 'group_admins';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Group admins';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['access']['perm'] = 'access user profiles';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['pager']['options']['items_per_page'] = '5';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Relationship: OG membership: OG membership from User */
$handler->display->display_options['relationships']['og_membership_rel']['id'] = 'og_membership_rel';
$handler->display->display_options['relationships']['og_membership_rel']['table'] = 'users';
$handler->display->display_options['relationships']['og_membership_rel']['field'] = 'og_membership_rel';
$handler->display->display_options['relationships']['og_membership_rel']['required'] = TRUE;
/* Relationship: OG membership: OG Roles from membership */
$handler->display->display_options['relationships']['og_users_roles']['id'] = 'og_users_roles';
$handler->display->display_options['relationships']['og_users_roles']['table'] = 'og_membership';
$handler->display->display_options['relationships']['og_users_roles']['field'] = 'og_users_roles';
$handler->display->display_options['relationships']['og_users_roles']['relationship'] = 'og_membership_rel';
$handler->display->display_options['relationships']['og_users_roles']['required'] = TRUE;
/* Field: User: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'users';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['label'] = '';
$handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE;
/* Sort criterion: User: Created date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'users';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Contextual filter: OG membership: Group ID */
$handler->display->display_options['arguments']['gid']['id'] = 'gid';
$handler->display->display_options['arguments']['gid']['table'] = 'og_membership';
$handler->display->display_options['arguments']['gid']['field'] = 'gid';
$handler->display->display_options['arguments']['gid']['relationship'] = 'og_membership_rel';
$handler->display->display_options['arguments']['gid']['default_action'] = 'default';
$handler->display->display_options['arguments']['gid']['default_argument_type'] = 'og_context';
$handler->display->display_options['arguments']['gid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['gid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['gid']['summary_options']['items_per_page'] = '25';
/* Filter criterion: User: Active */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'users';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = '1';
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: OG user roles: Role Name */
$handler->display->display_options['filters']['name']['id'] = 'name';
$handler->display->display_options['filters']['name']['table'] = 'og_role';
$handler->display->display_options['filters']['name']['field'] = 'name';
$handler->display->display_options['filters']['name']['relationship'] = 'og_users_roles';
$handler->display->display_options['filters']['name']['value'] = array(
  'administrator member' => 'administrator member',
);

/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block');
$handler->display->display_options['defaults']['hide_admin_links'] = FALSE;

Previewed with the node id of the group previously created as parameter: only shows user "test group admin"

amitaibu’s picture

Status: Needs review » Fixed

Committed, thanks!

tjhart87’s picture

Where was this committed? I'm not seeing this in 7.x-2.0-rc4. Was it only added to dev?

SebCorbin’s picture

Yes, it's in dev (btw, thanks Amitai :) )

tjhart87’s picture

Applied patch to rc4 and all is well. Great work guys!

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