You are here

public function ConfigForm::buildForm in Akamai 8.3

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/ConfigForm.php, line 112

Class

ConfigForm
A configuration form to interact with Akamai API settings.

Namespace

Drupal\akamai\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  if ($this
    ->isHttps() === FALSE) {
    $this
      ->httpsWarning();
  }
  $config = $this
    ->config('akamai.settings');

  // Link to instructions on how to get Akamai credentials from Luna.
  $luna_url = 'https://developer.akamai.com/introduction/Prov_Creds.html';
  $luna_uri = Url::fromUri($luna_url);
  $form['akamai_credentials_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Akamai CCU Credentials'),
    '#description' => $this
      ->t('API Credentials for Akamai. Someone with Luna access will need to set this up. See @link for more.', [
      '@link' => Link::fromTextAndUrl($luna_url, $luna_uri)
        ->toString(),
    ]),
  ];
  $options = [
    'file' => $this
      ->t('.edgerc file'),
  ];
  if ($this->moduleHandler
    ->moduleExists('key')) {
    $options['key'] = $this
      ->t('Key module');
  }
  $form['akamai_credentials_fieldset']['storage_method'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Credential storage method'),
    '#default_value' => $config
      ->get('storage_method') ?: 'file',
    '#options' => $options,
    '#required' => TRUE,
    '#description' => $this
      ->t('Credentials may be stored in an .edgerc file or using the Key module (if installed). See the README file for more information.'),
  ];
  $key_field_states = [
    'required' => [
      ':input[name="storage_method"]' => [
        'value' => 'key',
      ],
    ],
    'visible' => [
      ':input[name="storage_method"]' => [
        'value' => 'key',
      ],
    ],
    'optional' => [
      ':input[name="storage_method"]' => [
        'value' => 'file',
      ],
    ],
    'invisible' => [
      ':input[name="storage_method"]' => [
        'value' => 'file',
      ],
    ],
  ];
  $file_field_states = [
    'required' => [
      ':input[name="storage_method"]' => [
        'value' => 'file',
      ],
    ],
    'visible' => [
      ':input[name="storage_method"]' => [
        'value' => 'file',
      ],
    ],
    'optional' => [
      ':input[name="storage_method"]' => [
        'value' => 'key',
      ],
    ],
    'invisible' => [
      ':input[name="storage_method"]' => [
        'value' => 'key',
      ],
    ],
  ];
  $form['akamai_credentials_fieldset']['rest_api_url'] = [
    '#type' => 'url',
    '#title' => $this
      ->t('REST API URL'),
    '#description' => $this
      ->t('The URL of the Akamai CCU API host. It should be in the format *.purge.akamaiapis.net/'),
    '#default_value' => $config
      ->get('rest_api_url'),
    '#states' => $key_field_states,
  ];
  $keys = [];
  if ($this->keyProvider
    ->hasKeyRepository()) {
    foreach ($this->keyProvider
      ->getKeys() as $key) {
      $keys[$key
        ->id()] = $key
        ->label();
    }
  }
  asort($keys);
  $form['akamai_credentials_fieldset']['access_token'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Access Token'),
    '#description' => $this
      ->t('Access token.'),
    '#options' => $keys,
    '#default_value' => $config
      ->get('access_token'),
    '#states' => $key_field_states,
  ];
  $form['akamai_credentials_fieldset']['client_token'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Client Token'),
    '#description' => $this
      ->t('Client token.'),
    '#options' => $keys,
    '#default_value' => $config
      ->get('client_token'),
    '#states' => $key_field_states,
  ];
  $form['akamai_credentials_fieldset']['client_secret'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Client Secret'),
    '#description' => $this
      ->t('Client secret.'),
    '#options' => $keys,
    '#default_value' => $config
      ->get('client_secret'),
    '#states' => $key_field_states,
  ];
  $form['akamai_credentials_fieldset']['edgerc_path'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Path to .edgerc file'),
    '#default_value' => $config
      ->get('edgerc_path') ?: '',
    '#states' => $file_field_states,
  ];
  $form['akamai_credentials_fieldset']['edgerc_section'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Section of .edgerc file to use for the CCU API'),
    '#default_value' => $config
      ->get('edgerc_section') ?: 'default',
    '#states' => $file_field_states,
  ];
  $form['ccu_version'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('CCU Version'),
    '#default_value' => $config
      ->get('version') ?: 'v3',
    '#options' => array_map(function ($version) {
      return $version
        ->getPluginDefinition()['title'];
    }, $this->availableVersions),
    '#required' => TRUE,
    '#description' => $this
      ->t('Select which Akamai client version to use.'),
  ];
  foreach ($this->availableVersions as $id => $version) {
    $definition = $version
      ->getPluginDefinition();
    $form['akamai_version_settings']['#options'][$id] = $definition['title'];
    $form['akamai_version_settings'][$id] = [
      '#type' => 'details',
      '#title' => $this
        ->t('@version settings', [
        '@version' => $definition['title'],
      ]),
      '#open' => TRUE,
      '#tree' => TRUE,
      '#states' => [
        'visible' => [
          ':radio[name="ccu_version"]' => [
            'value' => $id,
          ],
        ],
      ],
    ];
    $form['akamai_version_settings'][$id] += $version
      ->buildConfigurationForm([], $form_state);
  }
  global $base_url;
  $basepath = $config
    ->get('basepath') ?: $base_url;
  $form['basepath'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Base Path'),
    '#default_value' => $basepath,
    '#description' => $this
      ->t('The URL of the base path (fully qualified domain name) of the site.  This will be used as a prefix for all cache clears (Akamai indexes on the full URI). e.g. "http://www.example.com"'),
    '#required' => TRUE,
  ];
  $form['timeout'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Timeout Length'),
    '#description' => $this
      ->t("The timeout in seconds used when sending the cache clear request to Akamai's servers. Most users will not need to change this value."),
    '#size' => 5,
    '#maxlength' => 3,
    '#default_value' => $config
      ->get('timeout'),
    '#required' => TRUE,
  ];
  $form['domain'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Domain'),
    '#default_value' => $this
      ->getMappingKey($config
      ->get('domain')),
    '#options' => [
      'production' => $this
        ->t('Production'),
      'staging' => $this
        ->t('Staging'),
    ],
    '#description' => $this
      ->t('The Akamai domain to use for cache clearing.'),
    '#required' => TRUE,
  ];
  $form['edge_cache_tag_header_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Edge-Cache-Tag Header Settings'),
  ];
  $form['edge_cache_tag_header_fieldset']['edge_cache_tag_header'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable Edge-Cache-Tag Header'),
    '#default_value' => $config
      ->get('edge_cache_tag_header'),
    '#description' => $this
      ->t('Sends Edge-Cache-Tag header in responses for Akamai'),
  ];
  $form['edge_cache_tag_header_fieldset']['edge_cache_tag_header_blacklist'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Cache Tag Blacklist'),
    '#default_value' => $config
      ->get('edge_cache_tag_header_blacklist'),
    '#description' => $this
      ->t('List of tag prefixes to blacklist from the Edge-Cache-Tag header. One per line.'),
    '#pre_render' => [
      [
        $this,
        'implodeElement',
      ],
    ],
  ];
  $form['edgescape_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Edgescape Settings'),
  ];
  $form['edgescape_fieldset'][Edgescape::EDGESCAPE_SUPPORT] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable Edgescape Support'),
    '#default_value' => $config
      ->get(Edgescape::EDGESCAPE_SUPPORT),
    '#description' => $this
      ->t('Enable support for Akamai Edgescape processing and Drupal token'),
  ];
  $form['devel_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Development Options'),
  ];
  $form['devel_fieldset']['disabled'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Disable all calls to Akamai'),
    '#default_value' => $config
      ->get('disabled'),
    '#description' => $this
      ->t('Killswitch - disable Akamai cache clearing entirely.'),
  ];
  $form['devel_fieldset']['log_requests'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Log requests'),
    '#default_value' => $config
      ->get('log_requests'),
    '#description' => $this
      ->t('Log all requests and responses.'),
  ];
  return parent::buildForm($form, $form_state);
}