0

I'm trying to insert a block inside of a node. The problem is that I want it to appeat automatically after a paragraph X, preferably after the first paragraph.

AdSense Injector module (http://drupal.org/project/adsense_injector) is quite useful, but it isn't very flexible, because you can only insert one code. Since I plan to insert different blocks in different situations, I would like to insert a region after a first paragraph.

There is a tutorial (www.werockyourweb.com/drupal-insert-adsense-ads-into-middle-of-content) that doesn't seem to work for Drupal 7.

Here is visual explanation:

<h1>Title</h1>
<p>Some text here</p>
<div>BLOCK INSIDE OF THE NEW REGION</div>
<p>Some text here</p>

Could someone please offer some guidelines?

EDIT:

Here are the codes I'm using.

Block code:

<h2>Is this working?</h2>

.info file:

regions[testing] = 'Testing'

Template.php

function THEMENAME_preprocess_node(&$variables) {

//load your adblock
$testing = block_load('block', '1');
$output .= drupal_render(_block_get_renderable_array(_block_render_blocks(array($testing))));
$variables['ad'] = $output; 
}

Node.tpl.php

<?php
$array = explode("</p>", $body[0]['value']);
$array[1] = $ad. $array[1];
$content['body'] = implode("</p>", $array);
print render($content['body']);
?>
take2
  • 616
  • 2
  • 17
  • 31
  • Are you using the code from the tutorial that's labeled as working in drupal 7? If so, what happens when you use it? "doesn't work" isn't very helpful for troubleshooting. – octern Oct 03 '12 at 16:48
  • Parse error: syntax error, unexpected $end in ...template.php on line 31. I have probably inserted the rest of the code wrong. – take2 Oct 03 '12 at 16:53

1 Answers1

1

The code from your link is:

$array = explode("", $body[0]['value']);
$array[1] = $ad. $array[1];
$content['body'] = implode("", $array);
print render($content['body']);

That looks like it should work, except I can't figure out why it's providing a blank delimiter for explode() . What if you ran explode and implode with the first parameter set to "</p>"?

octern
  • 4,825
  • 21
  • 38
  • Thanks for the answer. How exactly should I insert it? I added the region in .info file, it works. This code you added should be embedded in function THEMENAME_preprocess_node? Could you write the exact snippet? And what do you mean by "What if you ran explode and implode with the first parameter set to ""?" – take2 Oct 03 '12 at 16:52
  • Is $adblock the name of the region? Because I have changed "adblock" to the name of my new region. – take2 Oct 03 '12 at 16:58
  • I copied that code directly from the link you gave, so you should try doing exactly what it says. The only change I suggested is that instead of `implode("", $array)` (which is not even valid syntax and produces an error), you should use `implode("", $array)`, and the same for explode(). – octern Oct 03 '12 at 16:59
  • I have updated the code according to your suggestions. Now there aren't any errors, but still no output too. Could you check my code in the question? I have updated it with all of the codes. – take2 Oct 03 '12 at 18:24
  • It's attempting to insert the ad text after the first `` link in your node text. It looks like the node text you're using doesn't have any `` links, which is probably causing the problem. Try putting in some real example text. – octern Oct 03 '12 at 18:29
  • Ok, the problem was in this line in template.php "$testing = block_load('block', '1');" The number was different than 1. However, the whole node gets outputed once more under the original output, this time with this new region and that block. Is there a way to output only that region? And what exactly is $ad referring to? Thanks a lot for your help!! – take2 Oct 03 '12 at 18:43
  • Hm, the solution is to put that code ABOVE this line "
    >". I put it underneath it. So, the only answer I need now is what is this $ad referring to? :)
    – take2 Oct 03 '12 at 18:45
  • Unfortunately, I have realized now that this approach is actually very unflexible and not tied to the region. It will simply output the block with a certain number, no matter in which region that block is. I also can't control on which content types it will be outputed. In that case, it's less useful than AdSense injector module, unless you create a separate node.tpl.php for each content type. – take2 Oct 03 '12 at 19:03
  • You actually can write a template file that only affects a certain region. You can also put your customizations in node.tpl.php and put them inside a conditional so that they only fire when you're rendering the desired block. It sounds like you may need to get some more background in the templating system before you can implement these, though. I'm not an expert on it myself, so I don't know exactly where you'd need to look. – octern Oct 03 '12 at 20:02
  • What bothers me is that the new region is included in template.php, but it seems that there is no effect. – take2 Oct 03 '12 at 20:08