You are here

public function ManagedAd::getAdContent in Google AdSense integration 8

Return the ad content.

Return value

array ad content

Overrides AdsenseAdInterface::getAdContent

File

src/Plugin/AdsenseAd/ManagedAd.php, line 91

Class

ManagedAd
Provides an AdSense managed ad unit.

Namespace

Drupal\adsense\Plugin\AdsenseAd

Code

public function getAdContent() {
  if (!empty($this->format) && !empty($this->slot)) {
    $config = $this->configFactory
      ->get('adsense.settings');
    $client = PublisherId::get();
    $this->moduleHandler
      ->alter('adsense', $client);
    $defer = $config
      ->get('adsense_managed_defer');
    if (ManagedAd::isResponsive($this->format)) {
      $shape = $this->format == 'responsive' ? implode(',', $this->shape) : $this->format;

      // Responsive smart sizing code.
      $content = [
        '#theme' => 'adsense_managed_responsive',
        '#format' => $this->format,
        '#client' => $client,
        '#slot' => $this->slot,
        '#shape' => $shape,
        '#defer' => $defer,
      ];
    }
    elseif (ManagedAd::isFluid($this->format)) {
      $style = 'display:block';
      if ($this->format == 'in-article') {
        $style .= '; text-align:center;';
      }

      // Responsive smart sizing code.
      $content = [
        '#theme' => 'adsense_managed_fluid',
        '#format' => $this->format,
        '#client' => $client,
        '#slot' => $this->slot,
        '#layout_key' => $this->layoutKey,
        '#style' => $style,
        '#defer' => $defer,
      ];
    }
    else {

      // Get width and height from the format.
      list($width, $height) = $this
        ->dimensions($this->format);
      if ($config
        ->get('adsense_managed_async')) {

        // Asynchronous code.
        $content = [
          '#theme' => 'adsense_managed_async',
          '#format' => $this->format,
          '#width' => $width,
          '#height' => $height,
          '#client' => $client,
          '#slot' => $this->slot,
          '#defer' => $defer,
        ];
      }
      else {
        $lang = $config
          ->get('adsense_secret_language');
        $secret = $lang ? "    google_language = '{$lang}';\n" : '';

        // Synchronous code.
        $content = [
          '#theme' => 'adsense_managed_sync',
          '#format' => $this->format,
          '#width' => $width,
          '#height' => $height,
          '#client' => $client,
          '#slot' => $this->slot,
          '#secret' => $secret,
          '#defer' => $defer,
        ];
      }
    }
    return $content;
  }
  return [];
}