View source
<?php
namespace Drupal\adsense\Plugin\Block;
use Drupal\adsense\AdBlockInterface;
use Drupal\adsense\Plugin\AdsenseAd\CustomSearchAd;
use Drupal\adsense\Plugin\AdsenseAd\CustomSearchV2Ad;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CustomSearchAdBlock extends BlockBase implements AdBlockInterface, ContainerFactoryPluginInterface {
protected $configFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'));
}
public function defaultConfiguration() {
return [
'ad_slot' => '',
'version' => '',
];
}
public function createAd() {
$configuration = [
'slot' => $this->configuration['ad_slot'],
];
switch ($this->configuration['version']) {
case '1':
return new CustomSearchAd($configuration);
case '2':
default:
return new CustomSearchV2Ad($configuration);
}
}
public function build() {
return $this
->createAd()
->display();
}
public function blockForm($form, FormStateInterface $form_state) {
$form['label_display']['#default_value'] = FALSE;
$link = Link::fromTextAndUrl($this
->t('Google AdSense account page'), Url::fromUri('https://www.google.com/adsense/app#main/myads-springboard'))
->toString();
$form['ad_slot'] = [
'#type' => 'textfield',
'#title' => $this
->t('Ad ID'),
'#default_value' => $this->configuration['ad_slot'],
'#description' => $this
->t('This is the Ad ID from your @adsensepage, such as 1234567890.', [
'@adsensepage' => $link,
]),
'#required' => TRUE,
];
$default = $this->configuration['version'];
if (empty($this->configuration['version'])) {
$default = empty($this->configuration['ad_slot']) ? '2' : '1';
}
$form['version'] = [
'#type' => 'radios',
'#title' => $this
->t('CSE Version'),
'#default_value' => $default,
'#options' => [
'1' => $this
->t('Version 1'),
'2' => $this
->t('Version 2'),
],
'#description' => $this
->t('CSE version. If unsure, choose %default.', [
'%default' => 'Version 2',
]),
'#required' => TRUE,
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['ad_slot'] = $form_state
->getValue('ad_slot');
$this->configuration['version'] = $form_state
->getValue('version');
}
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), $this->configFactory
->get('adsense.settings')
->getCacheContexts());
}
public function getCacheTags() {
return Cache::mergeTags(parent::getCacheTags(), $this->configFactory
->get('adsense.settings')
->getCacheTags());
}
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
}