class CustomSearchAd in Google AdSense integration 8
Provides an AdSense custom search engine form.
Plugin annotation
@AdsenseAd(
id = "cse",
name = @Translation("CSE V1 Search"),
isSearch = TRUE,
needsSlot = TRUE,
version = 1
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\adsense\AdsenseAdBase implements AdsenseAdInterface, ContainerFactoryPluginInterface uses StringTranslationTrait
- class \Drupal\adsense\SearchAdBase
- class \Drupal\adsense\Plugin\AdsenseAd\CustomSearchAd
- class \Drupal\adsense\SearchAdBase
- class \Drupal\adsense\AdsenseAdBase implements AdsenseAdInterface, ContainerFactoryPluginInterface uses StringTranslationTrait
Expanded class hierarchy of CustomSearchAd
2 files declare their use of CustomSearchAd
- AdsenseCseSettings.php in src/
Form/ AdsenseCseSettings.php - CustomSearchAdBlock.php in src/
Plugin/ Block/ CustomSearchAdBlock.php
File
- src/
Plugin/ AdsenseAd/ CustomSearchAd.php, line 21
Namespace
Drupal\adsense\Plugin\AdsenseAdView source
class CustomSearchAd extends SearchAdBase {
/**
* Ad slot ID.
*
* @var string
*/
private $slot;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id = '', $plugin_definition = NULL, $config_factory = NULL, $module_handler = NULL, $current_user = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $config_factory, $module_handler, $current_user);
$this->slot = !empty($configuration['slot']) ? $configuration['slot'] : '';
}
/**
* {@inheritdoc}
*/
public function getAdPlaceholder() {
if (!empty($this->slot)) {
$client = PublisherId::get();
$content = "CSE v1\ncx = partner-{$client}:{$this->slot}";
return [
'#content' => [
'#markup' => nl2br($content),
],
'#format' => 'Search Box',
];
}
return [];
}
/**
* {@inheritdoc}
*/
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 [];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AdsenseAdBase:: |
protected | property | Config factory. | |
AdsenseAdBase:: |
protected | property | Current user. | |
AdsenseAdBase:: |
protected | property | Module handler. | |
AdsenseAdBase:: |
public static | function |
List of available languages. Overrides AdsenseAdInterface:: |
|
AdsenseAdBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
AdsenseAdBase:: |
public static | function |
Creates the ad object, as specified by the definitions in the parameter. Overrides AdsenseAdInterface:: |
|
AdsenseAdBase:: |
public | function | Display ad HTML. | |
AdsenseAdBase:: |
public static | function | Check if ads display is disabled. | |
CustomSearchAd:: |
private | property | Ad slot ID. | |
CustomSearchAd:: |
public | function |
Return the ad content. Overrides AdsenseAdInterface:: |
|
CustomSearchAd:: |
public | function |
Return the ad placeholder. Overrides AdsenseAdInterface:: |
|
CustomSearchAd:: |
public | function |
Creates a new AdsenseAdBase instance. Overrides AdsenseAdBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
SearchAdBase:: |
public static | function |
This is the array that holds all ad formats. Overrides AdsenseAdInterface:: |
|
SearchAdBase:: |
public static | function | Supply available countries for the search configuration forms. | |
SearchAdBase:: |
public static | function | Supply available encodings for the search configuration forms. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |