Hi Guys,

How can I control a block's visibility depending on the current module or URL. I have an ed-classifieds module.

If I click the Classifieds Link located in the primary menu then I want a block in the sidebar to appear. Then, in any other URL aside from ed-classified, my block will always be invisible.

Can someone please tell me a tutorial link or some PHP code to work on? Thanks :)

Cheers,
Mark

Comments

arh1’s picture

change the 'Page specific visibility settings' at the bottom of the block's configuration page.

if your site can use a URL structure where your Classifieds are at e.g. http://example.com/ed-classified/ad-title and other URLs do not start with http://example.com/ed-classified , then you can just set the visibility settings to 'Show on only the listed pages' and set the 'Pages' to:
ed-classified
ed-classified/*

if the URLs won't follow a simple pattern like that, you'll need a PHP snippet (and you'd need more to supply more info about how you're differentiating the classifieds from other site content to get specific suggestions for that).

marknt15’s picture

Thanks arh1. I was checking about that setting but I didn't know what will I put to the Pages: textarea. Now it works. Thanks again. :)

marknt15’s picture

Hi arh1,

I have a follow up question. How can I hide the secondary menus based on a given URL? For example I want my secondary menu to only appear when I am browsing the ed-classifieds? Other than that, my secondary menu will always be hidden.

Is there a setting like a 'Page specific visibility settings' in the secondary menu?

Thanks in advance :)

arh1’s picture

if you are printing the secondary links using the block (automatically) provided by the menu module, you can use the same interface as discussed above. if your theme is printing them directly, you'll need to add some custom logic via PHP...

something like this in template.php:

<?php
// this should go in template.php
function yourthemename_preprocess_page(&$vars, $hook) {
  // add your custom logic to the if statement below...
  if ($foo == 'bar') {
    $vars['secondary_links_visible'] = true;
  }
  else {
    $vars['secondary_links_visible'] = false;
  }
}
?>

and something like this in page.tpl.php (lifted/tweaked from Zen):

          <?php if ($secondary_links && $secondary_links_visible): ?>
            <div id="secondary">
              <?php print theme('links', $secondary_links); ?>
            </div> <!-- /#secondary -->
          <?php endif; ?>
marknt15’s picture

Thanks again arh1 :D
Through your help and this link: http://drupal.org/node/134425
I solved my problem. I'm new a newbie in drupal and I will straighten up each learning curve hehe (^__ _^)Y

This is my code.

function zen_classic_preprocess_page(&$vars, $hook) {
        // add your custom logic to the if statement below...
	
	// check if it is http or https
	$protocol = substr($_SERVER['SCRIPT_URI'], 0, 5);
	
	if ($protocol == 'https') {
		$protocol = 'https';
	} else {
		$protocol = 'http';
	}

	$match = FALSE;
	$current_url = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];
	
	if ($current_url == $_SERVER['SCRIPT_URI'] . '?q=ed-classified') {
		$match = TRUE;
	}
	if ($current_url == $_SERVER['SCRIPT_URI'] . '?q=node/add/ed-classified'){
		$match = TRUE;
	}
	
	if ($match === TRUE) {
		$vars['secondary_links_visible'] = true;
	} else {
		$vars['secondary_links_visible'] = false;
	}
}