I would like to display the primary links only to authenticated users on my drupal installation. I see where I can set specific blocks only to show for certain user roles. Is there a similar setting that I'm missing, or can anyone direct me to a spot in the code where I should make a modification?

Thanks,
Spencer

Comments

Chill35’s picture

Here's one way to do it :

(I assume that you don't want Primary Links to show for anonymous users in other words...)

STEP 1 : Open your page.tpl.php file in your theme folder

STEP 2 : Locate this code

<?php if (isset($primary_links)) : ?>
          <?php print theme('links', $primary_links, array('class' => 'links primary-links')) ?>
        <?php endif; ?>
        <?php if (isset($secondary_links)) : ?>
          <?php print theme('links', $secondary_links, array('class' => 'links secondary-links')) ?>
        <?php endif; ?>

SPEP 3 : change it to :

<?php global $user;
 if ($user->uid && isset($primary_links)) : ?>
          <?php print theme('links', $primary_links, array('class' => 'links primary-links')) ?>
        <?php endif; ?>
        <?php if (isset($secondary_links)) : ?>
          <?php print theme('links', $secondary_links, array('class' => 'links secondary-links')) ?>
        <?php endif; ?>

You may want to hide secondary links as well (as a matter of fact, I don't know if these work at all).

The admin and all authenticated users have user id, i.e. $user->uid, equal to 1 or higher... (admin = 1). Anonymous users have user id zero.

Caroline
11 heavens

armanjava’s picture

Would this work for users that have a 'pending authentication' role? They have registered on the site, but thanks to logintoboggan, I have this temporary role in place. They will be 'authenticated' once they verify their e-mail.

Chill35’s picture

It should work. As far as I understand, with that module, pending authentication users are logged in, it's just that they have a special role. So they do have their user id already, they just don't have the role 'authenticated'. So $user->uid is not zero for them.

Try it! (I am... 90% sure this will work LOL).

Caroline
11 heavens

armanjava’s picture

I was trying to find out if I can also limit the primary links for the 'pending verificatoin' users. Since they're logged in, I'm assuming they don't have a uid of '0'.. but would there be anyway to hide the primary links for anonymous users AND these other 'pending authentication' users?

VM’s picture

correct they don't have a UID of 0
0 = anon users

what is the UID of this "other role" ?

personally this seems like an awful lot of trouble, in D5 you can limit what menus show to user roles. This would be the approach I would take.

If you wanted to push through with this type of implementation, I think you would have have to think in terms of coding around the idea of roles rather then UID. Ill let Chill comment on this though, as the snippet is hers..

Chill35’s picture

Personally this seems like an awful lot of trouble, in D5 you can limit what menus show to user roles. This would be the approach I would take.

Where do you do that ? That indeed is the best solution. I think you are refering to 'Primary Links as a block' visibility though. Let me know.

By the way, let's not confuse uid and role. These guys who are not yet 'full members' are actually members -- all have their distinct, unique uid, and they are logged-in -- and they all have the same role. What's that role ? Check on the 'access control' page or logintoboggan's page to find out.

A note on primary links : they are either a block on the page or/and they are displayed by the theme (on page.tpl.php).

Using my theme (Garland), if I enable the Primary Links Block in the header (or anywhere else on the page), I have two "primary links' menus on the page for the price of one. My theme displays the primary links in page.tpl.php, always... The code snippet I gave you above is based on my theme, and restricts that display for logged-in users.

Whether your primary links are a block or they are displayed by your theme, you'll resort to different ways to set visibility.

In the case where primary links are displayed in the theme, you'll need to change page.tpl.php.
In the case where primary links are displayed in a block, you'll need to change Role specific visibility settings in admin/build/block/configure/menu/2 ('Page specific visibility settings'). That will be recorded in the database : you won't need to modify the code in any file.

Now if Primary Links are a menu item with 'visibility' settings that can be set by role, than this is definately the best solution. VeryMisunderstood, please let us know where we can set that (In drupal 5).

Thank you in advance :)

Caroline
11 heavens

VM’s picture

yes I meant block visibility per role not menu item per role. I habitually add my menus in blocks which allows me to give a menu block per role.

I don't use primary links very often at all, let alone for situations that involve visibility based on role.

armanjava’s picture

My Primary Links are part of my theme as well and I also am using Garland. Thus, I can't limit it with the 'menu setting's since it's not a block. Would there be an easy solution to limiting this by 'role'? I would want all anonymous users AND all of the 'pending authentication' users(which all have this same role) to be prohibited from seeing the 'primary links'. How could i do this by way of 'role' and not 'uid'?

VM’s picture

so the name of the role is "pending authentication" that you don't want to see the primary links, as well as anon role yes ?

armanjava’s picture

Yes, that's correct

Chill35’s picture

If primary links have to be shown only for authenticated users then... try this :

STEP 1 : Open your page.tpl.php file in your theme folder

STEP 2 : Locate this code

<?php if (isset($primary_links)) : ?>
          <?php print theme('links', $primary_links, array('class' => 'links primary-links')) ?>
        <?php endif; ?>
        <?php if (isset($secondary_links)) : ?>
          <?php print theme('links', $secondary_links, array('class' => 'links secondary-links')) ?>
        <?php endif; ?>

SPEP 3 : change it to :

<?php global $user;
if (in_array('authenticated user', $user->roles)) : ?>
        <?php print theme('links', $primary_links, array('class' => 'links primary-links')) ?>
        <?php endif; ?>
        <?php if (isset($secondary_links)) : ?>
          <?php print theme('links', $secondary_links, array('class' => 'links secondary-links')) ?>
        <?php endif; ?>

Caroline
Who am I in Drupal
11 heavens

shawnc’s picture

Hmm. What would you recommend I do? I looked in my 'themes' folder, as well as all of its subdirectories, and I cannot find any instance of page.tpl.php.

Thanks in advance for an help. Could you recommend a workaround, or a possible explanation as to why this file doesn't exist in my installation? I'm running Drupal 5.2.

VM’s picture

what theme is in use?

I'd guess chameleon or Marvin. if so, they aren't phptemplate themes, they are pure php themes.

_____________________________________________________________________
My posts & comments are usually dripping with sarcasm.
If you ask nicely I'll give you a towel : )

suede’s picture

Do you maybe have an idea how to control appearing of HOME in Marvin regarding the fact that it isn't a phptemplate theme? If so pls let me know :)

VM’s picture

Secondary links do indeed work. : )

the menu that is set as a primary link a must be a parent of children and be set to expanded in the menu item configuration. Then when the primary (parent) is active, the secondary (children) links will show.

spanders’s picture

Thanks, it worked perfect.

Chill35’s picture

I am glad that it works. You're welcome.

Hey Misunderstood, I learnt something : concerning secondary links. THANKSsss...

Caroline
11 heavens

dstanley333’s picture

Hi Caroline,

I want to do this very thing. However, the code above is not found in my page.tpl.php file at all. The only reference to the primary menu in my page.tpl.php file is as follows:

I am new to php... but have a test area where I can do this... what do you recommend I do?

VM’s picture

The thread is 5 years old which indicates that you are using a newer version of Drupal. This post is also in a deprecated forum. That said, it may be best to create a new thread in the post installation forum and to tag that thread with the version of Drupal in use.

silurius’s picture

Say I have "Home", "Private" and "Blogs" and want to hide "Private" for everyone save one specific user group. What would be the best/recommended means of controlling access to individual primary links?

silurius’s picture

Menu Per Role module is a thing of beauty.

suede’s picture

but is it applicable to Marvin theme?

eileenmcnaughton’s picture

it breaks stuff....

(menu per role that is - I don't know if my reply popped up in the right place)

cgjohnson’s picture

I'm having the opposite problem, in Marvin theme (drupal 4.7). When a new account is created, the primary links disappear for them -- and those are the main nav links! Why is that?? How can I fix that? (I'm using front_page module.)

any help is appreciated. thanks!

light-blue’s picture

I had the same problem as you, though without the front page module. It turns out that my authenticated users did NOT have access to the path I provided for my first (and only) primary link. Thus, Drupal did not show the link. I discovered this by creating a new primary link to nowhere in particular, and the link appeared.

brian_c’s picture

The standard Primary Links you generally see are provided directly by the Theme, independent of the Block system. I'm not sure why they did it this way instead of just sticking it in a Block, like they did for Navigation. Perhaps to give the theme developer greater control over exact placement of Primary Links, since blocks are user configured.

Anyway, if you need more control over the Primary Links, you can remove it from the Theme, and then add it back in as a Block, which you have much more control over, such as showing it only to certain roles (like Authenticated).

To remove Primary Links from the Theme (in Drupal 6 anyway, don't know about Drupal 5), go to Admin / Themes, and go to "configure" for the selected theme, then you can just uncheck Primary Links (and Secondary too if you want).

Then go to Admin / Blocks, find Primary Links, and set it to header or wherever you want it. You can then configure the Block however you like.