View source
<?php
namespace Drupal\adsense\Form;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\Factory\FactoryInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AdsenseManagedSettings extends ConfigFormBase {
protected $condition;
public function __construct(ConfigFactoryInterface $config_factory, FactoryInterface $plugin_factory) {
parent::__construct($config_factory);
try {
$this->condition = $plugin_factory
->createInstance('request_path');
} catch (PluginException $e) {
$this->condition = NULL;
}
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.condition'));
}
public function getFormId() {
return 'adsense_managed_settings';
}
protected function getEditableConfigNames() {
return [
'adsense.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('adsense.settings');
$form['adsense_managed_async'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use asynchronous ad code?'),
'#default_value' => $config
->get('adsense_managed_async'),
'#description' => $this
->t('This will enable the asynchronous ad code type. [@moreinfo]', [
'@moreinfo' => Link::fromTextAndUrl($this
->t('More information'), Url::fromUri('https://support.google.com/adsense/answer/3221666'))
->toString(),
]),
];
$form['adsense_managed_defer'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Defer javascript loading?'),
'#default_value' => $config
->get('adsense_managed_defer'),
'#description' => $this
->t('This will defer the execution of the ad script until the page is loaded'),
];
$form['adsense_managed_page_level_ads_enabled'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable auto ads?'),
'#default_value' => $config
->get('adsense_managed_page_level_ads_enabled'),
'#description' => $this
->t('This will enable Auto ads. [@moreinfo]', [
'@moreinfo' => Link::fromTextAndUrl($this
->t('More information'), Url::fromUri('https://support.google.com/adsense/answer/7478040'))
->toString(),
]),
];
$form['visibility'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Auto ads visibility'),
'#states' => [
'invisible' => [
":input[name='adsense_managed_page_level_ads_enabled']" => [
'checked' => FALSE,
],
],
],
];
if ($config
->get('adsense_access_pages')) {
$this->condition
->setConfiguration($config
->get('adsense_access_pages'));
}
$form['visibility'] += $this->condition
->buildConfigurationForm($form['visibility'], $form_state);
$form['visibility']['negate']['#type'] = 'radios';
$form['visibility']['negate']['#default_value'] = (int) $form['visibility']['negate']['#default_value'];
$form['visibility']['negate']['#title_display'] = 'invisible';
$form['visibility']['negate']['#options'] = [
$this
->t('Show for the listed pages'),
$this
->t('Hide for the listed pages'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->configFactory()
->getEditable('adsense.settings');
$form_state
->cleanValues();
$this->condition
->submitConfigurationForm($form, $form_state);
$config
->set('adsense_access_pages', $this->condition
->getConfiguration());
$values = $form_state
->getValues();
unset($values['pages']);
unset($values['negate']);
foreach ($values as $key => $value) {
$config
->set($key, Html::escape($value));
}
$config
->save();
parent::submitForm($form, $form_state);
}
}