You are here

public function AdsenseAdBase::display in Google AdSense integration 8

Display ad HTML.

Parameters

array $classes: Set of classes to add to the ad HTML.

Return value

array render array with ad or placeholder depending on current configuration.

File

src/AdsenseAdBase.php, line 120

Class

AdsenseAdBase
Base class for the AdsenseAd plugins.

Namespace

Drupal\adsense

Code

public function display(array $classes = []) {
  $config = $this->configFactory
    ->get('adsense.settings');
  $libraries = [
    'adsense/adsense.css',
  ];
  $text = '';
  if ($this
    ->isDisabled($text)) {
    return [
      '#type' => 'inline_template',
      '#template' => '<!-- adsense: {{ comment }} -->',
      '#context' => [
        'comment' => $text,
      ],
    ];
  }
  elseif ($config
    ->get('adsense_test_mode') || $this->currentUser
    ->hasPermission('show adsense placeholders')) {

    // Show ad placeholder.
    $content = $this
      ->getAdPlaceholder();
    return [
      '#theme' => 'adsense_ad',
      '#content' => $content['#content'],
      '#width' => isset($content['#width']) ? $content['#width'] : NULL,
      '#height' => isset($content['#height']) ? $content['#height'] : NULL,
      '#format' => $content['#format'],
      '#classes' => array_merge([
        'adsense-placeholder',
      ], $classes),
      '#attached' => [
        'library' => $libraries,
      ],
    ];
  }
  else {

    // Display ad-block disabling request.
    if ($config
      ->get('adsense_unblock_ads')) {
      $libraries[] = 'adsense/adsense.unblock';
    }
    $content = $this
      ->getAdContent();

    // Show ad.
    return [
      '#theme' => 'adsense_ad',
      '#content' => $content,
      '#width' => isset($content['#width']) ? $content['#width'] : NULL,
      '#height' => isset($content['#height']) ? $content['#height'] : NULL,
      '#format' => isset($content['#format']) ? $content['#format'] : NULL,
      '#classes' => $classes,
      '#attached' => [
        'library' => $libraries,
      ],
    ];
  }
}