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.
Comments
Ata Allah (not verified)
Drupal 4.7
Wed, 2005/11/09 - 02:12Greetings,
How to obtain Drupal 4.7 ?
At the site, tha latest version is 4.6.3
Regards,
Ata
Khalid
Not released yet
Tue, 2005/11/15 - 21:42It is not released yet.
You can experiment with the CVS version, which is still under development.
--
Khalid Baheyeldin
Budda (not verified)
Block titles not needed
Wed, 2006/02/08 - 05:58Block 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!
aoc gold (not verified)
Is this still the case or is
Fri, 2008/08/08 - 12:15Is this still the case or is there a more recent work around?
m3avrck (not verified)
Overriding regions example has one caveat...
Sun, 2006/02/19 - 13:25Hi 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!
G.M. Nowels (not verified)
block.tpl.php code loses regions
Mon, 2006/05/15 - 17:20When I add your block.tpl.php change to my theme I loose my footer and footer region.
Khalid
Define them all
Mon, 2006/05/15 - 17:50Define all the regions, not just the new ones.
This way, you do not lose any of them.
Qrios (not verified)
Adding to regions array in Drupal
Tue, 2006/12/05 - 08:50This works for me:
Regards
Dizwell (not verified)
Non-privileged users can set Adsense IDs
Thu, 2006/10/26 - 01:44First 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
Khalid
Private means for others to view
Thu, 2006/10/26 - 10:28Private here means that others would not be able to view the field value.
Anyone can enter a value though.
So this behaviour is expected.
--
Khalid Baheyeldin
Pages