You are here

public function CustomSearchAd::getAdContent in Google AdSense integration 8

Return the ad content.

Return value

array ad content

Overrides AdsenseAdInterface::getAdContent

File

src/Plugin/AdsenseAd/CustomSearchAd.php, line 58

Class

CustomSearchAd
Provides an AdSense custom search engine form.

Namespace

Drupal\adsense\Plugin\AdsenseAd

Code

public function getAdContent() {
  if (!empty($this->slot)) {
    $client = PublisherId::get();
    $this->moduleHandler
      ->alter('adsense', $client);
    $cse_config = $this->configFactory
      ->get('adsense.settings');
    $branding = $cse_config
      ->get('adsense_cse_logo');
    $results_path = Url::fromRoute('adsense_cse.results')
      ->toString();

    // @todo this is necessary for unclean URLs.

    /*  $results_path = $base_url;
        $hidden_q_field = '<input type="hidden" name="q" value="." />';*/
    $forid = 0;
    switch ($cse_config
      ->get('adsense_cse_ad_location')) {
      case 'adsense_cse_loc_top_right':
        $forid = 10;
        break;
      case 'adsense_cse_loc_top_bottom':
        $forid = 11;
        break;
      case 'adsense_cse_loc_right':
        $forid = 9;
        break;
    }
    if ($branding == 'adsense_cse_branding_watermark') {

      // When using a watermark, code is not reusable due to indentation.
      $content = [
        '#theme' => 'adsense_cse_watermark',
        '#language' => $cse_config
          ->get('adsense_cse_language'),
        '#results_path' => $results_path,
        '#client' => $client,
        '#slot' => $this->slot,
        '#forid' => $forid,
        '#encoding' => $cse_config
          ->get('adsense_cse_encoding'),
        '#qsize' => $cse_config
          ->get('adsense_cse_textbox_length'),
        '#search' => $this
          ->t('Search'),
      ];
    }
    else {
      $box_background_color = $cse_config
        ->get('adsense_cse_color_box_background');
      $content = [
        '#theme' => 'adsense_cse_branding',
        '#class' => $branding == 'adsense_cse_branding_right' ? 'cse-branding-right' : 'cse-branding-bottom',
        '#bg_color' => $box_background_color,
        '#color' => $box_background_color == '000000' ? 'FFFFFF' : '000000',
        '#results_path' => $results_path,
        '#client' => $client,
        '#slot' => $this->slot,
        '#forid' => $forid,
        '#encoding' => $cse_config
          ->get('adsense_cse_encoding'),
        '#qsize' => $cse_config
          ->get('adsense_cse_textbox_length'),
        '#search' => $this
          ->t('Search'),
        '#custom_search' => $this
          ->t('Custom Search'),
      ];
    }
    return $content;
  }
  return [];
}