[Drupa] Showing or hiding blocks in a Drupal site using custom logic with PHP

| | 2 min read

Blocks in Drupal can be assigned to regions in specified pages. Two options which are already in Drupal 7's block configuration page are:

  • All pages except those listed
  • Only the listed pages

And if you have enabled the PHP filter module, you may find another option:

  • Pages on which this PHP code returns TRUE (experts only)

In this article,I am going to explain to you, how you can display a block on pages based on the value returned by PHP code.


text-area

Method 1: Code inside the text-area

You can write your PHP code inside the text-area provided in the page(Refer the text-area.png).

But there are certain rules that you should follow while writing the php code.
Those are:

  • you should include the code between <?php ... ?>
  • The code should return either TRUE or FALSE.

Also please be aware that, an incorrect code can break your site.

As an example, I will show a sample code that can be written inside the text area.

<?php
  $condition = FALSE;
  if (test) { 
    $condition = TRUE;
  }
  return $match;
?>

If the above code is written inside the block configuration, the block will be displayed if the above test is true.

Method 2: Function inside the custom module

You can use the first condition, if your code is simple. Now, what if your condition is complicated? Suppose you have some database query to run as the condition,then?

In such cases, you can write a function inside your custom module and call this inside block configuration. Please note that the function should return either TRUE or FALSE.
The steps to be followed are:

step 1: Define a custom function inside your module. The function should either return TRUE or FALSE.

For eg:
<?php
function modulename_block_in_pages($nid) { 
  $condition = FALSE;         
  $node = node_load($nid);
  if (isset($product_node->attributes)) {
    $query = db_select('uc_product_attributes', 'up');
    $query->leftJoin('node', 'n', 'up.nid = n.nid');
    $query
    ->fields('up', array('aid'))
    ->distinct();
    $query->condition('n . type', 'product');
    $query->condition('n . nid', $nid);
    $rows = $query->execute();
    $records = $rows->fetchAll(); 
    foreach ($records as $record) {
      $aid = $record->aid; 
    }
    if ($product_node->attributes[$aid]->name == "test") {
      $condition = TRUE;
    }
  }
  // returns true if the node has an attribute with name 'test', else returns false
  return $condition;      .
}
?>

step 2 : Call the function in the block configuration page.

For eg:
<?php
  $nid = arg(0);
  $custom_block = modulename_block_in_pages($nid);
?>

Do note that this is not a recommended approach towards showing / hiding blocks using custom logic. You could write your own hook block and then write the logic to show / hide your blocks. You could also use context module to have much better control over the display of blocks on your site.

Hope this helps!!!