I am trying to turn off a block in drupal using existing code.

<?php
$node = node_load(arg(1));
$type = $node->type;

if(substr($_SERVER["REQUEST_URI"], 0, 13) == '/our-people')
{
return TRUE;
}
else
{
    return in_array($type,array('3_column_interior_page', '3_column_faculty_bio_page'));
}
?>

I wanted to add a page that will not show the block...

<?php
$node = node_load(arg(1));
$type = $node->type;

if(substr($_SERVER["REQUEST_URI"], 0, 13) == '/our-people')
{
return TRUE;
}
// new code here - I don't want this block to show up on the info sessions page.
elseif ($_SERVER["REQUEST_URI"], 0, 13) == '/information-sessions'
{
return FALSE;
}
// end new code
elseif
{
    return in_array($type,array('3_column_interior_page', '3_column_faculty_bio_page'));
}
?>

However it won't work. Being mostly a beginner, I am not sure how else to write it out.Any thoughts or a direction will help.

Thanks
Tim

Dani AI

Generated

Short diagnosis and a safe pattern to use: the thread shows two separate issues — a malformed elseif/condition in one reply, and logic-order (an earlier return stops later checks) in the original code. Use a robust path test (don’t depend on hard-coded substring lengths), test the "hide this page" case first, and use Drupal helpers so the code behaves correctly when not on a node page.

Here’s a compact, safer example you can drop into the block PHP visibility box (keeps query strings, trailing slashes and non-node pages in mind):

<?php
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

// Hide on /information-sessions (exact prefix, allows trailing slash)
if (preg_match('#^/information-sessions(?:/|$)#', $path)) {
  return FALSE;
}

// Always show on /our-people prefix
if (strpos($path, '/our-people') === 0) {
  return TRUE;
}

// Fallback: only show for specific node types
$node = menu_get_object(); // safer than node_load(arg(1))
if ($node && in_array($node->type, array('3_column_interior_page', '3_column_faculty_bio_page'))) {
  return TRUE;
}

return FALSE;
?>

Why this is better: parse_url removes query strings, preg_match handles trailing slashes and exact-prefix matching, and menu_get_object() avoids loading a node when the page isn’t a node. The order is important — a return ends execution, so checks that must block the block (return FALSE) need to come before checks that return TRUE. That explains why ’s final fix (moving the information-sessions check earlier) worked. was correct about the missing substring call; ’s snippet had an incomplete condition; ’s “change elseif to else” misses the real cause.

Final notes: clear Drupal caches after changing PHP visibility code. For production, avoid PHP-in-UI (security); implement the same logic in a small custom module (hook_block_view_alter or hook_block_info) instead.

Recommended Answers

All 5 Replies

change elseif in line 15 to else

Hey,

Try this:

<?php
  $node = node_load(arg(1));
  $type = $node->type;

  if(substr($_SERVER["REQUEST_URI"], 0, 13) == '/our-people')
  {
     return true;
  }else if(substr($_SERVER["REQUEST_URI"], 0, 13)) {
     return false;
}else{
  return in_array($type, array('3_column_interior_page', '3_column_faculty_bio_page'));
}

Thank you for the input. Neither solution worked though.

You missed the substr function in your elseif:

if(substr($_SERVER["REQUEST_URI"], 0, 13) == '/our-people')
{
  return TRUE;
}
elseif (substr($_SERVER["REQUEST_URI"], 0, 21) == '/information-sessions')
{
  return FALSE;
}
else
{
  return in_array($type,array('3_column_interior_page', '3_column_faculty_bio_page'));
}

Okay, I figurede it out.

    <?php
    $node = node_load(arg(1));
    $type = $node->type;

    if(substr($_SERVER["REQUEST_URI"], 0, 21) == '/information-sessions')
    {
    return true;
    }
    elseif(substr($_SERVER["REQUEST_URI"], 0, 13) == '/our-people')
    {
    return true;
    } else {
    return in_array($type, array('3_column_interior_page', '3_column_faculty_bio_page'));
    }
    ?>

The False one needed to be first.

Peace
Tim

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.