You are here

function _dfp_js_slot_definition in Doubleclick for Publishers (DFP) 7

Same name and namespace in other branches
  1. 7.2 dfp.module \_dfp_js_slot_definition()

Helper function to build the javascript needed to define an ad slot and add it to the head tag.

1 call to _dfp_js_slot_definition()
template_preprocess_dfp_tag in ./dfp.module
Preprocess function for DFP tags.

File

./dfp.module, line 745

Code

function _dfp_js_slot_definition($tag) {

  // Avoid accidentaly adding ad slot definition more than once.
  if (isset($tag->processed) && $tag->processed === TRUE) {
    watchdog('dfp', 'DFP tag %machinename is being added to the page more than once.', array(
      '%machinename' => $tag->machinename,
    ), WATCHDOG_WARNING);
    return;
  }

  // Add the js needed to define this adSlot to <head>.
  $js = '';

  // Start by defining breakpoints for this ad.
  if (!empty($tag->breakpoints)) {
    $breakpoints = $tag->breakpoints;
    $js .= 'var mapping = googletag.sizeMapping()' . "\n";
    foreach ($breakpoints as $breakpoint) {

      // If the ad sizes string contains the special keyword "<none>," use an
      // empty size list in order to suppress slot display.
      $ad_sizes = strpos($breakpoint['ad_sizes'], '<none>') !== FALSE ? '' : $breakpoint['ad_sizes'];
      $js .= '  .addSize(' . dfp_format_size($breakpoint['browser_size']) . ', ' . dfp_format_size($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";
  }
  else {
    $js .= 'googletag.slots["' . $tag->machinename . '"] = googletag.defineSlot("' . $tag->adunit . '", ' . $tag->size . ', "' . $tag->placeholder_id . '")' . "\n";
  }
  $click_url = variable_get('dfp_click_url', '');
  if (!empty($click_url)) {
    $js .= '  .setClickUrl("' . url($click_url, array(
      'absolute' => TRUE,
    )) . '")' . "\n";
  }
  $js .= '  .addService(googletag.pubads())' . "\n";
  if (!empty($tag->adsense_ad_types)) {
    $js .= '  .set("adsense_ad_types", "' . $tag->adsense_ad_types . '")' . "\n";
  }
  if (!empty($tag->adsense_channel_ids)) {
    $js .= '  .set("adsense_channel_ids", "' . $tag->adsense_channel_ids . '")' . "\n";
  }
  foreach ($tag->adsense_colors as $key => $val) {
    if (!empty($val)) {
      $key = 'adsense_' . $key . '_color';
      $val = '#' . drupal_strtoupper($val);
      $js .= '  .set("' . $key . '", "' . $val . '")' . "\n";
    }
  }
  $targeting = dfp_format_targeting($tag->targeting, $tag);
  foreach ($targeting as $target) {
    $js .= '  .setTargeting(' . $target['target'] . ', ' . $target['value'] . ')' . "\n";
  }

  // Apply size mapping when there are breakpoints.
  if (!empty($tag->breakpoints)) {
    $js .= '  .defineSizeMapping(mapping)' . "\n";
  }
  $js = rtrim($js, "\n") . ';';
  $options = array(
    'type' => 'inline',
    'group' => JS_LIBRARY,
    'weight' => 0,
    'force header' => TRUE,
    'scope_lock' => TRUE,
  );
  drupal_add_js($js, $options);
}