Skip to content

Commit

Permalink
Re-rolled patch
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelovani committed Apr 18, 2017
1 parent 10382c0 commit 4257154
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
43 changes: 37 additions & 6 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,9 +693,8 @@ 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";
}
else {
$js .= ' googletag.pubads().enableSyncRendering();' . "\n";
Expand Down Expand Up @@ -721,9 +727,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 +757,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 +805,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 +1008,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
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 4257154

Please sign in to comment.