You are here

public function HttpPurgerFormBase::buildFormHeaders in Generic HTTP Purger 8

Build the 'headers' section of the form.

Parameters

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

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

\Drupal\purge_purger_http\Entity\HttpPurgerSettings $settings: Configuration entity for the purger being configured.

1 call to HttpPurgerFormBase::buildFormHeaders()
HttpPurgerFormBase::buildForm in src/Form/HttpPurgerFormBase.php
Form constructor.

File

src/Form/HttpPurgerFormBase.php, line 239

Class

HttpPurgerFormBase
Abstract form base for HTTP based configurable purgers.

Namespace

Drupal\purge_purger_http\Form

Code

public function buildFormHeaders(array &$form, FormStateInterface $form_state, HttpPurgerSettings $settings) {
  if (is_null($form_state
    ->get('headers_items_count'))) {
    $value = empty($settings->headers) ? 1 : count($settings->headers);
    $form_state
      ->set('headers_items_count', $value);
  }
  $form['headers'] = [
    '#type' => 'details',
    '#group' => 'tabs',
    '#title' => $this
      ->t('Headers'),
    '#description' => $this
      ->t('Configure the outbound HTTP headers, leave empty to delete.'),
  ];
  $form['headers']['headers'] = [
    '#tree' => TRUE,
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Header'),
      $this
        ->t('Value'),
    ],
    '#prefix' => '<div id="headers-wrapper">',
    '#suffix' => '</div>',
  ];
  for ($i = 0; $i < $form_state
    ->get('headers_items_count'); $i++) {
    if (!isset($form['headers']['headers'][$i])) {
      $header = isset($settings->headers[$i]) ? $settings->headers[$i] : [
        'field' => '',
        'value' => '',
      ];
      $form['headers']['headers'][$i]['field'] = [
        '#type' => 'textfield',
        '#default_value' => $header['field'],
        '#attributes' => [
          'style' => 'width: 100%;',
        ],
      ];
      $form['headers']['headers'][$i]['value'] = [
        '#type' => 'textfield',
        '#default_value' => $header['value'],
        '#attributes' => [
          'style' => 'width: 100%;',
        ],
      ];
    }
  }
  $form['headers']['add'] = [
    '#type' => 'submit',
    '#name' => 'add',
    '#value' => $this
      ->t('Add header'),
    '#submit' => [
      [
        $this,
        'buildFormHeadersAdd',
      ],
    ],
    '#ajax' => [
      'callback' => [
        $this,
        'buildFormHeadersRebuild',
      ],
      'wrapper' => 'headers-wrapper',
      'effect' => 'fade',
    ],
  ];
}