background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

The Organic Groups Views Integration module comes with a bunch of useful views related to groups and their members. I assigned the "Recent members" block view to a region of my theme, and the block appeared on all group nodes and nodes that belong to a group. For the Drupal site I was working on, it was desired to only appear on the group nodes (not the nodes that belong to groups). No worries, you can always add block visibility code to determine when to show the block.

I went to the blocks admin screen (admin/build/block) and clicked configure for this block. Scroll down to the fieldset titled "Page specific visibility settings". I choose the radio option "Show if the following PHP code returns TRUE (PHP-mode, experts only)." and added the following PHP snippet:

<?php
if (function_exists('_MYMODULE_block_visibility_og_members'))
  return
_MYMODULE_block_visibility_og_members();
return
false;
?>

Next, I added a function in my module to handle the request.

<?php
function _MYMODULE_block_visibility_og_members() {
 
// lookup node id from query string
 
$path = drupal_lookup_path('source', $_REQUEST['q']);

 
// ensure the page is a node
 
if (substr($path,0,5)=='node/') {
   
// load node
   
$node = node_load(substr($path,5));

   
// check if node type is a group node
   
return og_is_group_type($node->type);
  }
  return
false;
}
?>

Now, the block only appears on group node types.

I recently created a block view that I wanted to include only on pages of a certain node type. I decided to create a PHP function that returns true or false based based on the page URL and then call the function via the "Page specific visibility settings", on the block edit screen.

This function returns true or false based on the what type of node the page URL is. I added this function to my template.php file, but you could add it to a module if desired.

<?php
function _MYTHEME_MYBLOCK_visibility() {
 
// look up the system path from the page URL
 
$path = drupal_lookup_path('source', $_REQUEST['q']);

 
// check if the page is a node
 
if (substr($path,0,5)=='node/') {
   
// load node
   
$node = node_load(substr($path,5));
       
   
// check if node exists
   
if (!$node) return false;
       
   
// check node type
   
if ($node->type == 'MYNODETYPE') return true;
       
  } else {
    return
false;   
  }
}
?>

Then I added a PHP snippet in the "Page specific visibility settings" on the block edit screen:

<?php
if (function_exists('_MYTHEME_MYBLOCK_visibility')) return _MYTHEME_MYBLOCK_visibility();
return
false;
?>

Now, the block only shows on pages of "MYNODETYPE".

Syndicate content