简体   繁体   English

Drupal使用带有CCK自定义字段的视图

[英]Drupal using views with CCK custom fields

I've got a Drupal site which uses a custom field for a certain type of node ( person_id ) which corresponds to a particular user. 我有一个Drupal网站,该网站使用自定义字段来表示对应于特定用户的某种类型的节点( person_id )。 I want to create a view so that when logged in, a user can see a list of nodes 'tagged' with their person_id . 我想创建一个视图,以便用户登录后可以看到带有“ person_id ”标签的节点列表。 I've got the view working fine, with a url of my-library/username but replacing username with a different username shows a list of all nodes tagged with that user. 我的视图工作正常,URL为my-library/username但用其他username my-library/username替换username名会显示该用户标记的所有节点的列表。 What I want to do is stop users changing the URL and seeing other users' tagged nodes. 我要做的是阻止用户更改URL,并查看其他用户的标记节点。 How can I do this? 我怎样才能做到这一点? Is there somewhere where I can dictate that the only valid argument for this page is the one that corresponds with the current logged in user's username? 是否可以在某处确定该页面的唯一有效参数是与当前登录用户的用户名相对应的参数?

person_id = uid? person_id = uid? In this case, add argument with user:uid, then in Validation options select PHP Code, read comment of this field carefully: 在这种情况下,请使用user:uid添加参数,然后在“验证”选项中选择“ PHP代码”,请仔细阅读此字段的注释:

Enter PHP code that returns TRUE or FALSE. 输入返回TRUE或FALSE的PHP代码。 No return is the same as FALSE, so be SURE to return something if you do not want to declare the argument invalid. 没有返回值与FALSE相同,因此如果您不想声明该参数无效,请确保返回值。 Do not use . 不使用 。 The argument to validate will be "$argument" and the view will be "$view". 验证的参数为“ $ argument”,视图为“ $ view”。 You may change the argument by setting "$handler->argument". 您可以通过设置“ $ handler-> argument”来更改参数。

Add this code: 添加此代码:

global $user;
$account = user_load('name'=>arg(1));
$handler->argument = $user->uid;
return $account->uid == $user->uid;

I'm not sure how you have setup your view, which gives some different options to solve this. 我不确定您如何设置视图,该视图提供了一些解决方案。 A way that should work would be to set the default argument be the logged users id/username and remove the argument from the url. 一种可行的方法是将默认参数设置为登录的用户ID /用户名,然后从url中删除该参数。

Alternatively you could create your own filter which requires some work with the views API, but gives more control. 另外,您可以创建自己的过滤器,该过滤器需要使用Views API,但可以提供更多控制权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM