You are here

public function VarnishAdminForm::buildForm in Varnish 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/VarnishAdminForm.php, line 59

Class

VarnishAdminForm

Namespace

Drupal\varnish\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('varnish.settings');

  // Decide whether or not to flush caches on cron runs.
  // $form['varnish_flush_cron'] = [
  //   '#type' => 'radios',
  //   '#title' => t('Flush page cache on cron?'),
  //   '#options' => [
  //     0 => $this->t('Disabled'),
  //     1 => $this->t('Enabled (with respect for cache_lifetime)'),
  //   ],
  //   '#default_value' => $config->get('varnish_flush_cron'),
  //   '#description' => $this->t('Internally Drupal will attempt to flush
  //     its page cache every time cron.php runs. This can mean too-frequent
  //     cache flushes if you have cron running frequently. NOTE: this cache
  //     flush is global!'),
  //  ];
  $form['varnish_version'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Varnish version'),
    '#default_value' => $config
      ->get('varnish_version'),
    '#description' => $this
      ->t('Select your varnish version.'),
    '#options' => [
      '3' => '3.x',
      '4' => '4.x',
    ],
  ];
  $form['varnish_control_terminal'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Varnish Control Terminal'),
    '#default_value' => $config
      ->get('varnish_control_terminal'),
    '#required' => TRUE,
    '#description' => $this
      ->t('Set this to the server IP or hostname that varnish runs on (e.g. 127.0.0.1:6082). This must be configured for Drupal to talk to Varnish. Separate multiple servers with spaces.'),
  ];
  $form['varnish_control_key'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Varnish Control Key'),
    '#default_value' => $config
      ->get('varnish_control_key'),
    '#description' => t('Optional: if you have established a secret key for control terminal access, please put it here.'),
  ];
  $form['varnish_socket_timeout'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Varnish connection timeout (milliseconds)'),
    '#default_value' => $config
      ->get('varnish_socket_timeout'),
    '#description' => $this
      ->t('If Varnish is running on a different server, you may need to increase this value.'),
    '#required' => TRUE,
  ];
  $form['varnish_cache_clear'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Varnish Cache Clearing'),
    '#options' => [
      '1' => $this
        ->t('Drupal Default'),
      '0' => $this
        ->t('None'),
    ],
    '#default_value' => $config
      ->get('varnish_cache_clear'),
    '#description' => $this
      ->t('What kind of cache clearing Varnish should utilize. Drupal default will clear all page caches on node updates and cache flush events. None will allow pages to persist for their full max-age; use this if you want to write your own cache-clearing logic.'),
  ];

  // Allow users to select Varnish ban type to use.
  $form['varnish_bantype'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Varnish ban type'),
    '#default_value' => $config
      ->get('varnish_bantype'),
    '#description' => $this
      ->t('Select the type of varnish ban you wish to use. Ban lurker support requires you to add beresp.http.x-url and beresp.http.x-host entries to the response in vcl_fetch.'),
    '#options' => [
      '0' => $this
        ->t('Normal'),
      '1' => $this
        ->t('Ban Lurker'),
    ],
  ];
  $status = [
    '#theme' => 'varnish_status',
    '#status' => varnish_get_status(),
    '#version' => floatval($config
      ->get('varnish_version')),
  ];

  // Check status.
  $form['varnish_stats'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('Status'),
    '#markup' => \Drupal::service('renderer')
      ->render($status),
  ];
  return parent::buildForm($form, $form_state);
}