Block visible for Specific Content Type AND Specific URL

I wanted to have a block displayed for a specific content type but also displayed on a number of pages.

The solution is to create a single Block and choose PHP for visibility settings.

The PHP code goes as follows:

<?php
$match
= FALSE;
$types = array('content-petites_annonces' => 1);
if (
arg(0) == 'node' && is_numeric(arg(1))) {
 
$nid = arg(1);
 
$node = node_load(array('nid' => $nid));
 
$type = $node->type;
  if (isset(
$types[$type])) {
   
$match = TRUE;
  }
}

if (
substr($_SERVER["REQUEST_URI"], 0, 10) == '/petitesan')
{
$match = TRUE;}

if (
substr($_SERVER["REQUEST_URI"], 0) == "/node/add/content-petites_annonces
"
)
{
$match = TRUE;}

return
$match;
?>

Basically the first part put $match to TRUE if the page is of the content type I want.
The other 2 if statements are selection based on the URL of the page. Allowing me to activate the Block for the URL I want.

Block visibility for node type and URL selection

george@dynapres.nl - July 25, 2007 - 08:11

Here's a more concise example which shows the block only on pages of specific node type and on the taxonomy pages. Tested on Drupal 5.x. Note the use of node_load argument and the arg() function for URL selection.

<?php
  $match
= FALSE;
 
$types = array('product' => 1);

  if ((
arg(0) == 'node') && is_numeric(arg(1))) {
   
$node = node_load(arg(1));
   
$match = isset($types[$node->type]);
  }

 
$match |= ((arg(0) == 'taxonomy') && (arg(1) == 'term'));

  return
$match;
?>

Nice example. I adapted it

cleaver - August 16, 2007 - 19:24

Nice example. I adapted it to show a block where either the URI is "/foo" or "/bar" or the node is of type "project". In the interest of platform independence, I used the request_uri() function.

If you needed to match a wildcard, this could be adapted by adding one of the regular expression functions.

<?php
  $match
= FALSE;
 
$types = array('project' => 1);
 
$uris = array('/foo' => 1, '/bar' => 1);

  if ((
arg(0) == 'node') && is_numeric(arg(1))) {
   
$node = node_load(arg(1));
   
$match = isset($types[$node->type]);
  }

 
$match |= isset($uris[request_uri()]);

  return
$match;
?>

Hide block on node edit page

WiredEscape - May 7, 2008 - 02:53

Here is an example for hiding a block on a node's edit page.

<?php
$match
= TRUE;
$url = request_uri();

if (
strpos($url, "edit")) {
 
$match = FALSE;
}
return
$match;
?>

Block visibility on other pages can be controlled by adding additional if statements. This example hides block on "/admin/*", "/filter/tips", "/node/add", and "node/10" pages.

<?php
$match
= TRUE;
$url = request_uri();

if (
strpos($url, "admin")) {
 
$match = FALSE;
}
if (
strpos($url, "filter/tips")) {
 
$match = FALSE;
}
if (
strpos($url, "node/add")) {
 
$match = FALSE;
}
if (
strpos($url, "edit")) {
 
$match = FALSE;
}
if (
strpos($url, "node/10")) {
 
$match = FALSE;
}
return
$match;
?>

I'm sure the code could be more efficient but would take away from showing the concept.

HTH,
Doug

 
 

Drupal is a registered trademark of Dries Buytaert.