Drupal: remove link to forum container

Drupal

For anonymous users that don't have access to forums, I don't see why they should be able to access the forum container (and the message "no forums defined"). In order to send an anonymous user to a 403 page on /forum I first tried what I thought was the easiest, Drupal's drupal_access_denied(). This still rendered the action links, so I hid those first.

<?php
function MYTHEME_preprocess_page(&$vars) {

  if (
user_is_anonymous() && arg(0) == 'forum') {
    unset(
$vars['action_links']);
   
drupal_access_denied();
  }

}
?>

However, this led to a slew of errors in the logs. Using drupal_goto() worked better:

<?php
function MYTHEME_preprocess_page(&$vars) {

 
/* --- Deny access to /forum for anonymous users --- */
 
if (user_is_anonymous() && arg(0) == 'forum') {
    unset(
$vars['page']->sidebar_first);
   
watchdog('access denied', '/forum');
   
drupal_goto('error/403', array('query'=>drupal_get_destination()));
  }

}
?>

Since it didn't log an error, I added that. Since the page is rendered as a regular node, I removed the side bar region. But that still left the link to /forum visible.

How is that for simple.

Then I found the best answer on Drupal, a simple hook_menu_alter modification (that after a slight modification I added to a small module with other stuff.) It removes the link and denies access:

<?php
function MYMODULE_menu_alter(&$items) {

 
// Check access for forum menu item.
 
$items['forum']['access callback'] = 'user_is_logged_in';

}
?>

'Access callback' determines whether a user will have access to the forum container.

See user_is_logged_in().

Comments

While we are at it (also in the same Drupal comments) in order to prevent access to /node I used

<?php
function MYMODULE_menu_alter(&$items) {

  unset(
$items['node']);

}
?>

which will result in a 404. Using the access callback you can trigger a 403:

<?php
function MYMODULE_menu_alter(&$items) {

 
$items['node']['access callback'] = FALSE;

}
?>

Personally, I prefer serving a 404 not found page.

To get back on topic, using <?php unset('forum') ?> will result in a "no forums defined" message. Exactly how this all started.

Using drupal_goto() worked better:(Nice way of using this code in excellent place to solve problem)

<?php
function MYTHEME_preprocess_page(&$vars) {

/* --- Deny access to /forum for anonymous users --- */
if (user_is_anonymous() && arg(0) == 'forum') {
unset($vars['page']->sidebar_first);
watchdog('access denied', '/forum');
drupal_goto('error/403', array('query'=>drupal_get_destination()));
}

}
?>

For Drupal services visit: https://

I am ltonser. I need help. What can i do it?

Don’t we all?