You are here

prebid.module in Doubleclick for Publishers (DFP) 7.2

File

prebid/prebid.module
View source
<?php

/**
 * @file
 */
define('PREBID_VERSION_DOCS', '1.0');
define('PREBID_VERSION', '1.0');
define('PREBID_JS_FILE', 'prebid.js');

/**
 * Implements hook_libraries_info().
 */
function prebid_libraries_info() {
  $url_text = t('Prebid @version Publisher API Changes', array(
    '@version' => PREBID_VERSION_DOCS,
  ));
  $url_api_path = 'http://prebid.org/dev-docs/prebid-' . PREBID_VERSION_DOCS . '-API.html';
  $libraries['prebidjs'] = array(
    'name' => 'Prebid.js',
    'vendor url' => 'http://prebid.org',
    'download url' => 'http://prebid.org/download.html',
    'git url' => 'https://github.com/prebid/Prebid.js.git',
    'version arguments' => array(
      'file' => PREBID_JS_FILE,
      'pattern' => '/v(\\d.+)/',
      'lines' => 1,
    ),
    'error message' => t('Prebid JS library must be installed and at minimum version @version. To download see the !download. For API documentation see !url', array(
      '@version' => PREBID_VERSION,
      '!download' => l(t('download page'), 'http://prebid.org/download.html'),
      '!url' => l($url_text, $url_api_path),
    )),
    'files' => array(
      'js' => array(
        PREBID_JS_FILE => array(
          'type' => 'file',
          'group' => JS_DEFAULT,
          'every_page' => TRUE,
          'weight' => -5,
          'force header' => TRUE,
        ),
      ),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_ctools_plugin_type().
 */
function prebid_ctools_plugin_type() {

  // Loads custom plugin types.
  ctools_include('utility');
  $items = array();
  ctools_passthrough('prebid', 'plugin-type', $items);
  return $items;
}

/**
 * Implements hook_ctools_plugin_api().
 *
 * Tell ctools that we support the prebid API.
 */
function prebid_ctools_plugin_api($owner, $api) {
  if ($owner == 'prebid' && $api == 'prebid') {
    return array(
      'version' => 1,
      'path' => drupal_get_path('module', 'prebid') . '/includes',
    );
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function prebid_preprocess_html(&$variables) {
  if (($library = libraries_load('prebidjs')) && !empty($library['loaded'])) {

    // We need to load the prebid.dfp.js file after prebid.js but before
    // dfp_googletag.cmd.js from DFP. See _dfp_js_global_settings().
    // Get the settings from the prebid.js library.
    $options = $library['files']['js'][PREBID_JS_FILE];

    // Up the weight by one so we are after prebid.js.
    $options['weight'] += 1;

    // Load our custom javascript.
    drupal_add_js(drupal_get_path('module', 'prebid') . '/js/prebid.dfp.js', $options);
    ctools_include('bidder', 'prebid');
    $bidders = prebid_get_bidders();
    $settings = array(
      'prebidSettings' => array(
        'bidders' => $bidders,
        'buckets' => prebid_get_price_granularity(),
        'global' => prebid_get_global_settings(),
        'adUnits' => array(),
      ),
    );
    drupal_add_js($settings, 'setting');
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function prebid_preprocess_dfp_tag(&$variables) {
  $tag = $variables['tag'];
  $name = $tag->machinename;
  ctools_include('bidder', 'prebid');
  $bidders = prebid_get_bidders();
  foreach ($bidders as $bidder) {
    $class = ctools_plugin_get_class($bidder, 'handler');
    if ($class) {
      $plugin = new $class();
      $plugin
        ->init($bidder, $tag);
    }
    else {
      $plugin = new stdClass();
      $plugin->plugin = $bidder;
    }

    // No, this does not overwrite the dfpTags settings but rather merges them.
    $settings = array(
      'dfpTags' => array(
        $name => array(
          'bidderAdapters' => array(
            $plugin->plugin,
          ),
        ),
      ),
    );
    drupal_add_js($settings, 'setting');
  }
}