Using Drupal 4.7 regions to show/hide AdSense

In a previous article, I described how one would use flexinode to display ads anywhere on the page, and not just blocks. This can be used to achieve things like restricting ads from showing on certain pages.

This can be achieved using Drupal 4.7 regions feature, which, like flexinode, allows multiple regions for blocks to be placed in. The default regions are left sidebar, right sidebar, header, footer and content.

However, blocks are always displayed with their title, which is ugly anywhere apart from the sidebars. Although CSS can be used to hide the header, this is more of a hack than the proper way of doing things.

Initial Attempts

Initially, I tried the following code in page.tpl.php:

<?php
$block = block_block('view', 1); // Block 1
drupal_set_content('content', $block['content']);
?>

The problem is that it will not obey the visibility rules of the block. Even if the block is disabled, it will still show in the following code. If it is to be shown only on non-admin pages, this rule will be ignored, and it will show everywhere. Not exactly what I wanted it to do.

The Proper Way

After corresponding with Nedjo Rogers, the author of 4.7's regions feature, and a lot of experimentation, here is the solution that can be used. This assumes that you have a PHPTemplate based theme.

Change the theme's block.tpl.php so that it looks like so:

<?php
switch($block->region) {
case 'ad_top':
case 'ad_bottom': ?>
<div class="<?php print $block->region; ?>"><?php print $block->content; ?></div>
<?php
break;
default: ?>
<div class="block block-<?php print $block->module;?>"
id="block-<?php print $block->module;?>-<?php print $block->delta;?>">
<h2 class="title"><?php print $block->subject; ?></h2>
<div class="content"><?php print $block->content; ?></div>
</div>
<?php
break;
} ?> 

Notice that for the ad_top and ad_bottom regions, only the block's content is displayed, and the region is used as the div's class.

Create a template.php or edit it, and add the following function to it:

<?php
function bluemarine_regions() {
return array(
'ad_top' => t('ad top'),
'ad_bottom' => t('ad bottom')
);

This defines two new regions for your theme, ad top and ad bottom. You can add as many regions as you want.

Create new blocks, put in them the call to the adsense_display() function with the arguments you want, and assign them to the regions you created.

Now, you can use simple statements to display the ads in your page.tpl.php or node.tpl.php anywhere you want, for example:

<?php print $ad_top; ?>
<?php print $ad_bottom; ?> 

Although the technique I mentioned uses Adsense as an example, it can be used to display anything. For example, you can use code snippets from the Drupal web site and display them anywhere you want.

Contents: 

Comments

Drupal 4.7

Greetings,

How to obtain Drupal 4.7 ?
At the site, tha latest version is 4.6.3

Regards,
Ata

Not released yet

It is not released yet.

You can experiment with the CVS version, which is still under development.
--
Khalid Baheyeldin

Block titles not needed

Block titles are not required in Drupal 4.7

So in the block.tpl.php simply do a if($block->subject) around the printing of the block title.

If no title is defined, nothing gets printed to the page. Works a treat!

Overriding regions example has one caveat...

Hi Khalid, there seems to be one slight problem with defining regions as bluemarine_regions() ...

If you do that, your site *only* has 2 regions now, and the default left, right, content, header, and footer are gone!

What should happen is you should copy the entire phptemplate_regions() and then append your regions, otherwise you can get some funky results.

Great article though!

block.tpl.php code loses regions

When I add your block.tpl.php change to my theme I loose my footer and footer region.

Non-privileged users can set Adsense IDs

First of all, thank you for a very nice module that behaves admirably. Second, I am sorry if this is not the right place to ask a question, and if there is a better place please let me know.

My issue is that, after having installed the Adsense module, and specifically after having made the Profile a 'private field, content only available to privileged users', ordinary, non-privileged users can see a tab that lets them access the Adsense Client ID field whenever they edit their profile. When they do so, they get a message to the effect that "the content of this field is kept private" -but they can nevertheless enter a value there, and click 'Submit'.

Checking with the contents of the PROFILE_VALUES table in MySQL, the 'submit' button really does add an entry into that table.

So my questions really are: (1) should all non-privileged users be able to even see the Adsense tab in their profiles? If not, what do I do to fix the privilege issue? (2) if they make an entry there and end up adding rows to the PROFILE_VALUES table, does that affect *my* adsense configuration and revenue-raising capabilties?

If it turns out the answers are (1) it doesn't matter if they can see the option, so long as they can't edit your entry and (2) so long as your entry is fine, your adsense revenues will be unaffected, I'd still like to request some mechanism for hiding the adsense field from users completely.

But I imagine it's my fault it's visible at all, and I would very much like to know what I've done wrong!

Regards
HJR

Pages