2011-08-02 3 views
0

私はdrupalサイトにいくつかのモデレータロールを持っています。このロールを持つユーザは、Newsという特定のコンテンツタイプのコンテンツを作成できます。 role_a、role_b、role_c、...Drupal:ビュー内のクエリ文字列データを使用

私は最後の5つのNews要素を表示しているビューを持っています。

私の質問は、クエリの文字列に応じてビュー内のNews要素を粒状化する方法ですか? 私はhttp://mysite.com/aのページにあります。ユーザが "a"ロールで追加したニュースだけを見たいと思っています。 http://mysite.com/bは、「b」ユーザのためのものです。

ビューフィルタでクエリ文字列パラメータを使用するにはどうすればよいですか?

答えて

1

あなたは、クエリ文字列ではなく引数を使用したいと思います。いずれにしても、私はViewがデフォルトでロール名を扱うことはできないと考えています(ロール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とし、結果を得るべきではありません)。

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; 
     } 
    } 
    } 
} 
関連する問題