Place Google Adsense or html/js code snippets as extra rows into View results

Place Google Adsense or html/js code snippets into View results

Introduction

When it comes to monetizing traffic on your websites,  placing the Google Adsense code snippets has always been easy. Usually, the process is simple: the ad's code snippet has to be put into content block and then placed anywhere on site, or just hardcoded directly into site's templates.

What about Views?

However, if you have a list of posts built with a regular view and you want to periodically show some ad between them, the task requires some coding. As for me, it has always been a case, because I could not find any suitable simple Drupal module that would resolve this task. So after all, in order to get rid of routine coding, I created a module called Views Row Insert for myself and then shared it with a Drupal community on Drupal.org.

Description

This module serves exacty this simple task: place extra rows with user's content from blocks or custom textarea field right between view results after nth row or with some other user defined logic. The inserted content is wrapped into a separate <div> row with custom class name so it is easy to theme it after all for the front end side.

Using the module

After you download and enable the module, go to the view display settings and click on Format style as it shown on the screen below.

Select format style of the view
Select format style of the view.


Select "Row Insert" format and click Apply.

Select Row Insert format
Select Row Insert format.


After clicking Apply, the plugin settings page will pop up, where you can see various output settings which should cover the most cases.
Here on the image I have my own settings with custom Google Ad block example.

Plugin settings page with custom settings
Plugin settings page with custom Google Ad block selection.

Insert Blocks

As you can see from the settings page above, you can choose to insert blocks generated by views and other modules, or insert custom content block with your Google ad code (make sure you save the block with Full HTML Text format enabled, otherwise the code will be stripped out).

Textarea Example

If you do not want to use blocks, you can place the code right into textarea field by selecting "Custom content" option. I will add some <script> tags in order to test our output. This field is unrestricted and passes all HTML markup to the output, so you have to be careful.

Textare example
Textarea example.


As a result, you will get the output with the following html markup.

Resulting HTML markup
Resulting HTML markup.


Our JS code also works, as we can see it in console output.

Console output
Console output.

Conclusion

So this is how you can insert extra custom rows containing Google Ad code or other blocks/content or code snippets into your view rows output with Views Row Insert module.

The module is available for Drupal 7 and Drupal 8 versions, you can find more information on the module's project page on drupal.org website. 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.