0

For a particular content type education i need a field to be hidden for the node author. It works in the template file but not with preprocess. This is my code

 function aapka_preprocess_node(&$variables){
     $node = $variables['node'];
     if ($node->type == 'education'){
     if ($node->uid == $user->uid && $user->uid != 1) {
 unset($variables['content']['field_college']);}
  }

What am i doing wrong??

2
  • Can you try to do this functionality in a node template file ? note--education.tpl.php , just copy paste it from the normal node.tpl.php and adjust it accordingly May 3 '14 at 0:49
  • It works in a template file am just looking for it to work in preprocess.
    – jack
    May 3 '14 at 0:52
0

You are referencing $user without bringing it into scope.

e.g using your current approach you could do this:

function aapka_preprocess_node(&$variables){
  // Bring global user object into scope
  global $user;
  // Assign by reference
  $node =& $variables['node'];
  if ($node->type == 'education'){
    if ($node->uid == $user->uid && $user->uid != 1) {
      unset($variables['content']['field_college']);
    }
  }
}

Keep in mind that won't work if the node is cached and the cache doesn't vary between the author and other regular registered users.

1
  • Thanks it works. Am only using default drupal cache for anonymous users. Also is it better to do this in the node template file than preprocess??
    – jack
    May 3 '14 at 17: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.