Drupal 6 Drupal:在视图中使用查询字符串数据
我的drupal站点中有几个版主角色。具有此角色的用户可以创建称为新闻的特定内容类型的内容。让我们将角色称为以下角色:角色a、角色b、角色c 现在我有一个视图显示了最后5个新闻元素 我的问题是如何根据查询字符串对视图中的新闻元素进行粒度化? 我的意思是,在页面上,我只想看到由具有“a”角色的用户添加的新闻。适用于“b”角色用户。等等Drupal 6 Drupal:在视图中使用查询字符串数据,drupal-6,views,Drupal 6,Views,我的drupal站点中有几个版主角色。具有此角色的用户可以创建称为新闻的特定内容类型的内容。让我们将角色称为以下角色:角色a、角色b、角色c 现在我有一个视图显示了最后5个新闻元素 我的问题是如何根据查询字符串对视图中的新闻元素进行粒度化? 我的意思是,在页面上,我只想看到由具有“a”角色的用户添加的新闻。适用于“b”角色用户。等等 如何在视图过滤器中使用查询字符串参数?我想您的意思是希望使用参数,而不是查询字符串。在任何情况下,我认为视图在默认情况下都不能处理角色名称(它可以很好地处理角色ID
如何在视图过滤器中使用查询字符串参数?我想您的意思是希望使用参数,而不是查询字符串。在任何情况下,我认为视图在默认情况下都不能处理角色名称(它可以很好地处理角色ID),因此您必须修改视图查询以实现所需的功能 首先,在视图中添加User:Roles作为参数。然后,在自定义模块中,实现hook\u views\u query\u 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是,那么它将查找角色“a”的ID,然后查找具有该角色的作者的所有节点。它还将采用实际的角色ID—例如,如果角色“a”的ID为10,则也将返回相同的结果
如果您只想让它查找角色名,那么可以修改钩子,使其在找不到角色时失败(只需将$rid=0,您就不会得到任何结果)
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)) {`enter code here`
// 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;
}
}
}
}