我认为你的意思是你想使用一个参数,而不是查询字符串。无论如何,我不认为 Views 可以默认处理角色名称(它可以很好地处理角色 ID),因此您必须修改您的视图查询才能实现您想要的。
首先,在视图中添加用户:角色作为参数。然后,在自定义模块中,实现 hook_views_query_alter() 并通过将角色名称替换为其角色 ID 来修改查询。
function MYMODULE_views_query_alter(&$view, &$query) {
if ($view->name == 'my_view') {
$rolename = '';
foreach ($query->where as $where_index => $where) {
// find the role ID clause
$clause_index = array_search('users_roles.rid = %d', $where['clauses']);
if ($clause_index !== FALSE) {
// found it, so get the rolename
$rolename = $where['args'][$clause_index];
break;
}
}
// if the rolename argument was found
if (!empty($rolename)) {
// get the role ID
$user_roles = user_roles();
$rid = array_search($rolename, $user_roles);
// if the role exists, then replace the argument
if ($rid !== FALSE) {
$query->where[$where_index]['args'][$clause_index] = $rid;
}
}
}
}
因此,例如,如果您的 url 是http://mysite.com/a,那么它将查找角色“a”的 ID,然后查找具有该角色的作者的所有节点。它还将采用实际的角色 ID - 例如,如果角色“a”的 ID 为 10,则 http://mysite.com/10 也将返回相同的结果。
如果您只希望它查找角色名称,您可以修改钩子以在未找到角色时失败(只需使 $rid = 0 并且您不应该得到任何结果)。