I know just enough PHP to get myself into trouble, so I figured I should post this simple task that I can't quite get right, and hope for some kind, generous assistance...

I wish to display the navigation block only to users who are logged in. Normal users will use only primary nav, including one link to the login page. In admin/blocks/nav I have the option to show the navigation block "if the following PHP code returns TRUE," and I can't figure out how to create a functional bit of PHP here.

I have messed around with various $user and uid statements after searching through the support forums, but none have worked. I would just choose the specific pages that the block would show on, but that is simply not feasible.

Comments

styro’s picture

<?php
  return $user->uid;
?>

Note: I haven't actually tested that, but anon users generally have a uid of 0 (php evaluates that as false) while logged in users have a positive integer uid (evaluates as true).

Same result, but being more explicit:

<?php
  if ($user->uid == 0) {
    return false;
  }
  else {
    return true;
  }
?>

This is slightly less efficient as it does more, but that probably makes very little difference inthe end.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ
Example Knowledge Base built using Drupal

kirklimon’s picture

I have already tried similar approaches to your suggestion, and I just tested out your code and variations, but the nav menu still appears for users who are not logged in.

I am wondering if there is some special way to phrase a true PHP statement (ie. some snippet that wouldn't be a normal php function or whatever), or if something is possibly funky with Drupal or the theme (this is my first project with 4.7 and I'm using the Meta theme as a foundation for my work).

heine’s picture

http://drupal.org/node/64854
--
When your problem is solved, please post a follow-up to the thread you started.

styro’s picture

Explanation:

The global statement is needed in most places in Drupal to access the user object. I would've noticed that if I had've bothered to actually test it.

And just to be more correct, the integer uid from $user->uid is cast into a boolean type using (bool)  ie actually true or false rather than just evaluating to something like true or false.

I recommend using the new snippet in the handbook :)

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ
Example Knowledge Base built using Drupal

kirklimon’s picture

Works like a charm... I used your code verbatim.

http://drupal.org/node/64854

sjfarrar’s picture

I was having this problem with a block too, and after trying a bunch of php statements, I stumbled upon this page: http://drupal.org/node/64135 has the perfect answer for a number of situations :)