Skip to content

Commit

Permalink
rerolled patch
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelovani committed Apr 27, 2017
1 parent 64ca210 commit 9ff08d3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
47 changes: 40 additions & 7 deletions dfp.module
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function dfp_theme($existing, $type, $theme, $path) {
'dfp_tag' => array(
'variables' => array(
'tag' => NULL,
'display_call' => NULL,
'slug' => NULL,
),
'template' => 'theme/dfp_tag',
Expand Down Expand Up @@ -675,6 +676,12 @@ function _dfp_js_global_settings() {
'weight' => -10,
'force header' => TRUE,
);
// Adds async attribute. Depends on this patch https://www.drupal.org/files/issues/js_attributes-1664602-116.patch.
// Covered in this issue https://www.drupal.org/node/1664602.
// Alternatively you can use Advagg + Advagg_mod installed.
if (variable_get('dfp_async_rendering', 1)) {
$options['async'] = TRUE;
}
drupal_add_js($js, $options);

// Include a script tag for the Google Tag Services.
Expand All @@ -686,12 +693,13 @@ function _dfp_js_global_settings() {

// Add global settings with a heavy weight so that they appear after all the
// defineSlot() calls otherwise IE8 and Opera fail to display ads properly.
$js = 'googletag.cmd.push(function() {' . "\n";
if (variable_get('dfp_async_rendering', 1)) {
$js .= ' googletag.pubads().enableAsyncRendering();' . "\n";
$js = 'googletag.cmd.push(function() {' . "\n";
$js .= 'googletag.pubads().enableAsyncRendering();' . "\n";

}
else {
$js .= ' googletag.pubads().enableSyncRendering();' . "\n";
$js = 'googletag.pubads().enableSyncRendering();' . "\n";
}
if (variable_get('dfp_single_request', 1)) {
$js .= ' googletag.pubads().enableSingleRequest();' . "\n";
Expand Down Expand Up @@ -721,9 +729,12 @@ function _dfp_js_global_settings() {
}
}

$js .= '});' . "\n";
$js .= ' googletag.enableServices();' . "\n";
if (variable_get('dfp_async_rendering', 1)) {
$js .= '});' . "\n";
}

$js .= variable_get('dfp_injected_js2', '') . "\n";
$js .= 'googletag.enableServices();';

$options = array(
'type' => 'inline',
Expand All @@ -748,15 +759,21 @@ function _dfp_js_slot_definition($tag) {

// Add the js needed to define this adSlot to <head>.
$js = '';
// Add the js needed to define this adSlot to <head>.
if (variable_get('dfp_async_rendering', 1)) {
$js .= 'googletag.cmd.push(function() {' . "\n";
}

// Start by defining breakpoints for this ad.
if (!empty($tag->breakpoints)) {
$breakpoints = $tag->breakpoints;
$js .= 'var mapping = googletag.sizeMapping()' . "\n";
$js .= ' var mapping = googletag.sizeMapping()' . "\n";
foreach ($breakpoints as $breakpoint) {
$js .= ' .addSize(' . dfp_format_size($breakpoint['browser_size']) . ', ' . dfp_format_size($breakpoint['ad_sizes']) . ')' . "\n";
}
$js .= ' .build();' . "\n";
}

if (!empty($tag->settings['out_of_page'])) {
$js .= 'googletag.slots["' . $tag->machinename . '"] = googletag.defineOutOfPageSlot("' . $tag->adunit . '", "' . $tag->placeholder_id . '")' . "\n";
}
Expand Down Expand Up @@ -790,7 +807,12 @@ function _dfp_js_slot_definition($tag) {
if (!empty($tag->breakpoints)) {
$js .= ' .defineSizeMapping(mapping)' . "\n";
}
$js = rtrim($js, "\n") . ';';
if (variable_get('dfp_async_rendering', 1)) {
$js = rtrim($js, "\n") . '});' . "\n";
}
else {
$js = rtrim($js, "\n") . ';' . "\n";
}

$options = array(
'type' => 'inline',
Expand Down Expand Up @@ -988,6 +1010,17 @@ function template_preprocess_dfp_tag(&$variables) {
);
}

// If async option is true wrap the display call in cmd.push.
if (variable_get('dfp_async_rendering', 1)){
$variables['display_call'] =
"googletag.cmd.push(function() {
googletag.display(\"" . $tag->placeholder_id . "\");
});\n";
}
else {
$variables['display_call'] = "googletag.display(\"" . $tag->placeholder_id . "\");\n";
}

// Define a javascript ad slot for this tag.
_dfp_js_slot_definition($tag);

Expand Down
5 changes: 5 additions & 0 deletions tests/dfp_globals.test
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class dfpGlobalsTest extends dfpBaseTest {

function testGlobalSettingsOn() {
$injected_js = $this->randomName(32);
$injected_js2 = $this->randomName(32);
$target = array(
'target' => $this->randomName(8),
'value' => $this->randomName(8),
Expand All @@ -28,12 +29,15 @@ class dfpGlobalsTest extends dfpBaseTest {
variable_set('dfp_single_request', '1');
variable_set('dfp_collapse_empty_divs', '1');
variable_set('dfp_injected_js', $injected_js);
variable_set('dfp_injected_js2', $injected_js2);
variable_set('dfp_targeting', array($target));
$this->drupalGet('/');
$this->assertRaw('googletag.cmd.push(function() {', 'Asyncronous rendering is turned on.');
$this->assertRaw('googletag.pubads().enableAsyncRendering();', 'Asyncronous rendering is turned on.');
$this->assertRaw('googletag.pubads().enableSingleRequest();', 'Single request is turned on.');
$this->assertRaw('googletag.pubads().collapseEmptyDivs();', 'Collapse empty divs is turned on.');
$this->assertRaw($injected_js, 'Injected javascript correctly appears on the page.');
$this->assertRaw($injected_js2, 'Injected javascript 2 correctly appears on the page.');
$this->assertRaw('googletag.pubads().setTargeting("' . $target['target'] . '", "' . $target['value'] . '");', 'Global targeting values appear correctly in javascript.');
}

Expand All @@ -42,6 +46,7 @@ class dfpGlobalsTest extends dfpBaseTest {
variable_set('dfp_single_request', '0');
variable_set('dfp_collapse_empty_divs', '0');
$this->drupalGet('/');
$this->assertRaw('googletag.pubads().enableSyncRendering();', 'Syncronous rendering is turned on.');
$this->assertNoRaw('googletag.pubads().enableAsyncRendering();', 'Asyncronous rendering is turned off.');
$this->assertNoRaw('googletag.pubads().enableSingleRequest();', 'Single request is turned off.');
$this->assertNoRaw('googletag.pubads().collapseEmptyDivs();', 'Collapse empty divs is turned off.');
Expand Down
8 changes: 4 additions & 4 deletions theme/dfp_tag.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<?php if (isset($slug)):
print drupal_render($slug);
endif; ?>
<script type="text/javascript">
googletag.cmd.push(function() {
googletag.display("<?php print $tag->placeholder_id ?>");
});
<?php if (isset($display_call)):?>
<script type="text/javascript">
<?php print ($display_call);?>
</script>
<?php endif; ?>
</div>

0 comments on commit 9ff08d3

Please sign in to comment.