You are here

public function AdsenseManagedSettings::buildForm in Google AdSense integration 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/AdsenseManagedSettings.php, line 73

Class

AdsenseManagedSettings
Form for the adsense managed ads settings.

Namespace

Drupal\adsense\Form

Code

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(),
    ]),
  ];

  // Auto ads visibility.
  $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);
}