You are here

adsense.module in Google AdSense integration 8

Displays Google AdSense ads on Drupal pages.

This is the core module of the AdSense package, with the Drupal hooks and other administrative functions.

File

adsense.module
View source
<?php

/**
 * @file
 * Displays Google AdSense ads on Drupal pages.
 *
 * This is the core module of the AdSense package, with the Drupal hooks
 * and other administrative functions.
 */
use Drupal\adsense\AdsenseAdBase;
use Drupal\adsense\ContentAdBase;
use Drupal\adsense\PublisherId;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_page_attachments().
 */
function adsense_page_attachments(array &$attachments) {
  $config = \Drupal::config('adsense.settings');
  if ($config
    ->get('adsense_managed_page_level_ads_enabled')) {

    /** @var \Drupal\system\Plugin\Condition\RequestPath $condition */
    $condition = \Drupal::getContainer()
      ->get('plugin.manager.condition')
      ->createInstance('request_path');
    $visibility = $config
      ->get('adsense_access_pages');
    if ($visibility) {
      $condition
        ->setConfiguration($visibility);
    }
    if ($visibility['negate'] xor !$condition
      ->evaluate()) {
      $attachments['#attached']['html_head'][] = [
        [
          '#type' => 'inline_template',
          '#template' => '<!-- adsense auto ads: {{ comment }} -->',
          '#context' => [
            'comment' => 'page not in match list',
          ],
        ],
        'adsense_unmatched_page',
      ];
    }
    elseif (AdsenseAdBase::isDisabled()) {
      $attachments['#attached']['html_head'][] = [
        [
          '#type' => 'inline_template',
          '#template' => '<!-- adsense auto ads: {{ comment }} -->',
          '#context' => [
            'comment' => 'ads disabled',
          ],
        ],
        'adsense_ads_disabled',
      ];
    }
    else {
      $client = PublisherId::get();
      \Drupal::moduleHandler()
        ->alter('adsense', $client);
      $attachments['#attached']['html_head'][] = [
        [
          '#type' => 'inline_template',
          '#template' => '',
          '#theme' => 'adsense_managed_page_level',
          '#client' => $client,
        ],
        'adsense_managed_page_level',
      ];
    }
  }
}

/**
 * Implements hook_theme().
 */
function adsense_theme() {
  return [
    'adsense_ad' => [
      'variables' => [
        'content' => '',
        'width' => '',
        'height' => '',
        'format' => '',
        'classes' => [],
      ],
    ],
    'adsense_cse_branding' => [
      'variables' => [
        'class' => '',
        'bg_color' => '',
        'color' => '',
        'results_path' => '',
        'client' => '',
        'slot' => '',
        'forid' => '',
        'encoding' => '',
        'qsize' => '',
        'search' => '',
        'custom_search' => '',
      ],
    ],
    'adsense_cse_results' => [
      'variables' => [
        'width' => '',
        'country' => '',
        'script' => '',
      ],
    ],
    'adsense_cse_watermark' => [
      'variables' => [
        'language' => '',
        'results_path' => '',
        'client' => '',
        'slot' => '',
        'forid' => '',
        'encoding' => '',
        'qsize' => '',
        'search' => '',
      ],
    ],
    'adsense_cse_v2_results' => [
      'variables' => [
        'client' => '',
        'slot' => '',
      ],
    ],
    'adsense_cse_v2_searchbox' => [
      'variables' => [
        'client' => '',
        'slot' => '',
      ],
    ],
    'adsense_managed_async' => [
      'variables' => [
        'format' => '',
        'width' => '',
        'height' => '',
        'client' => '',
        'slot' => '',
        'defer' => FALSE,
      ],
    ],
    'adsense_managed_page_level' => [
      'variables' => [
        'client' => '',
        'defer' => FALSE,
      ],
    ],
    'adsense_managed_responsive' => [
      'variables' => [
        'format' => '',
        'client' => '',
        'slot' => '',
        'shape' => '',
        'defer' => FALSE,
      ],
    ],
    'adsense_managed_fluid' => [
      'variables' => [
        'format' => '',
        'client' => '',
        'slot' => '',
        'layout_key' => '',
        'style' => '',
        'defer' => FALSE,
      ],
    ],
    'adsense_managed_sync' => [
      'variables' => [
        'format' => '',
        'width' => '',
        'height' => '',
        'client' => '',
        'slot' => '',
        'secret' => '',
        'defer' => FALSE,
      ],
    ],
  ];
}

/**
 * Implements template_preprocess_HOOK().
 */
function template_preprocess_adsense_ad(&$variables) {
  $variables['adclasses'][] = 'adsense';
  if (!empty($variables['format']) && (empty($variables['width']) || empty($variables['height']))) {
    list($variables['width'], $variables['height']) = ContentAdBase::dimensions($variables['format']);
  }
  if (empty($variables['content'])) {
    $variables['content'] = '<-- empty ad content -->';
  }
  if (!empty($variables['width']) && !empty($variables['height'])) {
    $variables['style'] = "width:{$variables['width']}px;height:{$variables['height']}px;";
  }
  else {
    $variables['adclasses'][] = 'responsive';
  }
  $variables['classes'] = implode(' ', $variables['classes']);
  $variables['adclasses'] = implode(' ', $variables['adclasses']);
}

/**
 * Implements hook_help().
 */
function adsense_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.adsense':
      module_load_include('inc', 'adsense', 'help/adsense.help');
      return adsense_help_text();
  }
  return '';
}

/**
 * Generates the Google AdSense Ad.
 *
 * @param array $args
 *   An array of arguments (format, group, channel or slot).
 *   A valid format must always be provided. If a slot is provided, the ad is
 *   generated by the new format modules, if not then the 'old' format modules
 *   are attempted.
 *
 * @return array
 *   Render array with ad code.
 *
 * @throws \Drupal\Component\Plugin\Exception\PluginException
 *   Exception thrown in the event of problems with the plugin.
 */
function adsense_display(array $args) {
  if (!is_array($args)) {

    // 'old' method of calling this function is not supported in version >= 8.x
    // adsense_display($format, $group, $channel, $slot, $referral, $cpa).
    $text = 'old adsense_display call syntax not supported';
  }
  else {
    $ad = AdsenseAdBase::createAd($args);
    if (isset($ad)) {
      return $ad
        ->display();
    }
    else {
      $text = 'no ad generated.';
    }
  }
  return [
    '#type' => 'inline_template',
    '#template' => '<!-- adsense: {{ comment }} -->',
    '#context' => [
      'comment' => $text,
    ],
  ];
}

/**
 * Implements hook_preprocess_block().
 */
function adsense_preprocess_block(&$variables) {

  // Remove adsense from the block id and classes to bypass adblock rules.
  if ($variables['configuration']['provider'] == 'adsense' && \Drupal::config('adsense.settings')
    ->get('adsense_unblock_ads')) {
    $variables['plugin_id'] = str_replace('adsense', '', $variables['plugin_id']);
    $variables['configuration']['provider'] = '';
  }
}

Functions

Namesort descending Description
adsense_display Generates the Google AdSense Ad.
adsense_help Implements hook_help().
adsense_page_attachments Implements hook_page_attachments().
adsense_preprocess_block Implements hook_preprocess_block().
adsense_theme Implements hook_theme().
template_preprocess_adsense_ad Implements template_preprocess_HOOK().