2

Drupal 6 Nodes + CCK allow the creation of custom fields on nodes and per Role based Permissions to view / edit / delete on a per Field basis. Whilst I don't want to simply "switch off" fields (disable the Permission to View a field based on a Role), I want the field to display "********", or "withheld".

So essentially I want to add additional permissions to the View CCK field, called "View Censored", which doesn't allow them to see the content, but know at least the content exists. In addition, it means that I don't have to have duplicate Views (one with full details, one with some sort of templated *** instead of fields) and the linked Nodes will display, but with censored fields automatically.

2

The only way I can think of to do this without making two views (which would be my preference to be honest) would be to use the Views Customfield module and use a PHP snippet to check permissions and display the result accordingly.

Create your view as normal and when choosing the field that you wish to change, make sure you check the bock that says "Exclude from display". Then add a custom PHP field directly after it and use code along these lines:

<?php
  // Check the logged in user
  global $user;
  // Check if the user has a certain role
  if (in_array('USER ROLE', array_values($user->roles))) {
    print $data->FIELDNAME;
  }
  else {
    print "Withheld";
  }
?>

Obviously you'll need to replace 'USER ROLE' with the role that you're looking for and you'll need to replace $data->FIELDNAME with whatever the Views fieldname is, for example $data->nid or $data->title.

Otherwise you could use a View with two slightly different block displays, embedded in a contextual Panel that checks for users permissions and displays accordingly.

Edit after asker comment: Craig, a few things. Firstly, I don't think you're approaching the problem correctly at all. What you want to do is difficult and a outside of the scope of your question - you asked how to alter a value on a view, not show two different values globally depending on permissions. Before you can get the solution you need to ensure you're tackling the right problem. You simply need to hide a value in two situations depending on a permission, nothing more.

Secondly, I'm not going to give you another solution as I have given you a perfectly good solution to your question. This may not be perfect for you but with some initiative you could very easily solve this problem with what I've given you. If you persevere with this information you can use the Content Templates module and the same code as above to hide the value on a node.

Thirdly, never hack modules. If you want to change the behaviour of a module, write another module to alter it.

| improve this answer | |
  • Thanks Jamie, but I need the rendered elements themselves to come back as censored, otherwise when / if I click on a link that takes me to the record itself (node/x) it will still show the details/hide the record field entirely / or fail with permission issues. I need it to detect that the user doesn't have the permission and simply mark it out.<br /> I had an idea of editing the CCK module in 6, adding "view uncensored" permissions, updating the user access hook with, then editing the render hook to show *** unless they had view && view uncensored permissions... – Craig Moore Jul 15 '11 at 12:16
  • See edited answer. – Jamie Hollern Jul 15 '11 at 13:35
  • All fair comments Jamie, I appreciate them. It is certainly not my first choice to attempt to hack a module I am using, there are upgrade/versioning issues etc, though it seems for what I want, a fairly good workaround. As for Views, your answer should be feasible. Many thanks! – Craig Moore Jul 15 '11 at 15:23
  • 2
    I worked out how to redact content - I used hook_nodeapi and then switch($op) looking for view. I checked to see the type of content, e.g. strcmp($node->type, 'job'), and global $user; if (is_array($user->roles) && in_array("subscribed_recruiter", $user->roles)) I then redacted field by field e.g. $node->field_cv_job_title[0]['value'] = 'hidden'; $node->field_cv_job_title[0]['safe'] = 'hidden'; and $node->content['field_cv_job_title']['field']['items'][0]['#item']['value'] = 'hidden'; $node->content['field_cv_job_title']['field']['items'][0]['#item']['safe'] = 'hidden'; Hope this helps! – Craig Moore Jul 18 '11 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.