From 9ff08d38c137c3e0a17fe74d11b88db39ee5baa5 Mon Sep 17 00:00:00 2001 From: Marcelo Vani Date: Thu, 27 Apr 2017 22:20:13 +0100 Subject: [PATCH] rerolled patch --- dfp.module | 47 +++++++++++++++++++++++++++++++++++------- tests/dfp_globals.test | 5 +++++ theme/dfp_tag.tpl.php | 8 +++---- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/dfp.module b/dfp.module index 55acbfa..5b442c4 100755 --- a/dfp.module +++ b/dfp.module @@ -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', @@ -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. @@ -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"; @@ -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', @@ -748,15 +759,21 @@ function _dfp_js_slot_definition($tag) { // Add the js needed to define this adSlot to . $js = ''; + // Add the js needed to define this adSlot to . + 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"; } @@ -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', @@ -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); diff --git a/tests/dfp_globals.test b/tests/dfp_globals.test index d020888..fcbd58b 100644 --- a/tests/dfp_globals.test +++ b/tests/dfp_globals.test @@ -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), @@ -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.'); } @@ -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.'); diff --git a/theme/dfp_tag.tpl.php b/theme/dfp_tag.tpl.php index 0dd89d7..b2d893f 100644 --- a/theme/dfp_tag.tpl.php +++ b/theme/dfp_tag.tpl.php @@ -9,9 +9,9 @@ - +