Drupal:在视图中使用查询字符串数据

发布于 2024-11-27 20:31:44 字数 373 浏览 0 评论 0原文

我在我的 drupal 网站上有多个版主角色。具有此角色的用户可以创建称为新闻的特定内容类型的内容。让我们将角色称为以下角色:role_a、role_b、role_c...

现在我有一个显示最后 5 个新闻元素的视图。

我的问题是如何根据查询字符串对视图中的新闻元素进行粒度化? 我的意思是在页面 http://mysite.com/a 上我只想查看由具有“a”角色的用户。 http://mysite.com/b 适用于“b”角色的用户。等等,

我如何在视图过滤器中使用查询字符串参数?

i have several moderator roles in my drupal site. the users with this roles can create content of specific content-type called News. let's call the roles the following: role_a, role_b, role_c, ...

now i have a View that shows the last 5 News elements.

my question is how to granulate the News elements in View according to the query string?
i mean on page http://mysite.com/a i want to see only the news that was added by the user with the "a" role. http://mysite.com/b is for the "b"-roled user. etc.

how can i use the query string parameters in the Views filter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空气里的味道 2024-12-04 20:31:44

我认为你的意思是你想使用参数,而不是查询字符串。无论如何,我认为 Views 默认情况下不能处理角色名(它可以很好地处理角色 ID),因此您必须修改视图查询才能实现您想要的效果。

首先,将 User: Roles 添加为视图中的参数。然后,在自定义模块中,实现 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;
      }
    }
  }
}

因此,例如,如果您的网址是 http://mysite.com/a,那么它将查找角色“a”的 ID,然后查找具有该角色的作者的所有节点。它还将采用实际的角色 ID - 例如,如果角色“a”的 ID 为 10,则 http://mysite .com/10 也会返回相同的结果。

如果您只想查找角色名,则可以修改挂钩,使其在找不到角色时失败(只需使 $rid = 0 并且您不应该得到任何结果)。

I think you mean you want to use an Argument, rather than the query string. In any case, I don't think Views can handle rolenames by default (it can handle role IDs just fine), so you'll have to modify your view query in order to achieve what you want.

First, add User: Roles as an argument in your View. Then, in a custom module, implement hook_views_query_alter() and modify the query by replacing the rolename with its role 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;
      }
    }
  }
}

So, for example, if your url is http://mysite.com/a, then it will look up the ID of role 'a', then find all nodes by an author with that role. It will also take the actual role ID - for example, if the ID of role 'a' is 10, then http://mysite.com/10 will also return the same result.

If you want it only to look up rolenames, you can modify the hook to fail when it doesn't find the role (just make $rid = 0 and you shouldn't get any results).

讽刺将军 2024-12-04 20:31:44
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;
      }
    }
  }
}
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;
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文