You are here

public function SettingsForm::submitForm in S3 File System 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Form/SettingsForm.php \Drupal\s3fs\Form\SettingsForm::submitForm()
  2. 8.2 src/Form/SettingsForm.php \Drupal\s3fs\Form\SettingsForm::submitForm()

Form submission handler.

Parameters

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

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

Overrides ConfigFormBase::submitForm

File

src/Form/SettingsForm.php, line 558

Class

SettingsForm
Defines a form that configures s3fs settings.

Namespace

Drupal\s3fs\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  if (!class_exists('\\Doctrine\\Common\\Cache\\FilesystemCache')) {
    $values['use_credentials_cache'] = FALSE;
    $values['credentials_cache_dir'] = '';
  }

  // Bucket region detection.
  if ($this->configFactory
    ->get('s3fs.settings')
    ->hasOverrides('region')) {
    $region = $this->configFactory
      ->get('s3fs.settings')
      ->get('region');
  }
  else {
    if (!empty($values['use_customhost']) && !empty($values['hostname'])) {

      // S3Client must always have a region, use a safe default.
      $region = S3fsService::DEFAULT_S3_REGION;
    }
    else {

      // Let the api get the region for the specified bucket.
      // Make sure we always have a safe region set before calling AWS.
      $values['region'] = S3fsService::DEFAULT_S3_REGION;
      $testClientConfig = $values;
      $testClientConfig['keymodule'] = [];
      if ($this->moduleHandler
        ->moduleExists('key')) {
        if (!empty($values['keymodule_access_key_name']) && !empty($values['keymodule_secret_key_name'])) {
          $testClientConfig['keymodule']['access_key_name'] = $values['keymodule_access_key_name'];
          $testClientConfig['keymodule']['secret_key_name'] = $values['keymodule_secret_key_name'];
        }
        unset($testClientConfig['keymodule_access_key_name']);
        unset($testClientConfig['keymodule_secret_key_name']);
      }
      $client = $this->s3fs
        ->getAmazonS3Client($testClientConfig);
      try {
        $region = $client
          ->determineBucketRegion($values['bucket']);
        if (!$region) {

          // Error with trying to determine region, use the default.
          $region = S3fsService::DEFAULT_S3_REGION;
        }
      } catch (\Exception $e) {
        $region = S3fsService::DEFAULT_S3_REGION;
      }
    }
  }
  $config = $this
    ->config('s3fs.settings');
  $config
    ->set('credentials_file', $values['credentials_file'])
    ->set('use_credentials_cache', $values['use_credentials_cache'])
    ->set('credentials_cache_dir', rtrim($values['credentials_cache_dir'], '/'))
    ->set('bucket', $values['bucket'])
    ->set('region', $region)
    ->set('use_cname', $values['use_cname'])
    ->set('domain_root', $values['domain_root'])
    ->set('use_customhost', $values['use_customhost'])
    ->set('hostname', UrlHelper::stripDangerousProtocols($values['hostname']))
    ->set('domain', $values['domain'])
    ->set('use_path_style_endpoint', $values['use_path_style_endpoint'])
    ->set('disable_version_sync', $values['disable_version_sync'])
    ->set('cache_control_header', $values['cache_control_header'])
    ->set('encryption', $values['encryption'])
    ->set('use_https', $values['use_https'])
    ->set('ignore_cache', $values['ignore_cache'])
    ->set('read_only', $values['read_only'])
    ->set('disable_cert_verify', $values['disable_cert_verify'])
    ->set('disable_shared_config_files', $values['disable_shared_config_files'])
    ->set('redirect_styles_ttl', $values['redirect_styles_ttl'])
    ->set('use_cssjs_host', $values['use_cssjs_host'])
    ->set('cssjs_host', $values['cssjs_host'])
    ->set('root_folder', trim($values['root_folder'], '\\/'))
    ->set('public_folder', trim($values['public_folder'], '\\/'))
    ->set('private_folder', trim($values['private_folder'], '\\/'))
    ->set('presigned_urls', $values['presigned_urls'])
    ->set('saveas', $values['saveas'])
    ->set('torrents', $values['torrents']);
  if ($this->moduleHandler
    ->moduleExists('key')) {
    $config
      ->set('keymodule.access_key_name', $values['keymodule_access_key_name'])
      ->set('keymodule.secret_key_name', $values['keymodule_secret_key_name']);
  }
  $config
    ->save();
  parent::submitForm($form, $form_state);
}