Drupal: Tweaking block visibility for content types and URLs

For the relaunch of reclaimyourcity.net, I recently developed a custom pager module that provides a block with links to the previous and next picture according to the particular gallery. The visibility settings for the block are restricted to the content type of the street art pictures. However, the pager also tries to show up on the node add/edit pages which is not very useful there. Based on the posts in this thread about block visibility at drupal.org, adding the following PHP validation prevents the block from showing on the node create/edit pages.

<?php
$show = TRUE;
$url = request_uri();
$hide_on_pages = array('/edit', '/add');

foreach($hide_on_pages as $page){
    if(substr($url, -strlen($page)) === $page) {
        $show = FALSE;
    }
}
return $show;
?>

It is possible to move the check for the content type into the validation as well. Here is a version that shows the block only for the specified content type with the additional page filter.

<?php
$show = FALSE;
$url = request_uri();
$content_type = 'picture';
$hide_on_pages = array('/edit', '/add');

if(arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
    $node = node_load($nid);
    $type = $node->type;
    if($type === $content_type) {
        $show = TRUE;
    }
}
foreach($hide_on_pages as $page){
    if(substr($url, -strlen($page)) === $page) {
        $show = FALSE;
    }
}
return $show;
?>
Posted in code Tagged with: , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*