public function S3fsCorsAdminForm::submitForm in S3 File System CORS Upload 8
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/ S3fsCorsAdminForm.php, line 106
Class
- S3fsCorsAdminForm
- Config settings for S3FS Cors.
Namespace
Drupal\s3fs_cors\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$cors_origin = $form_state
->getValue('s3fs_cors_origin');
$this
->config('s3fs_cors.settings')
->set('s3fs_cors_origin', $cors_origin)
->set('s3fs_https', $form_state
->getValue('s3fs_https'))
->set('s3fs_access_type', $form_state
->getValue('s3fs_access_type'))
->save();
// parent::submitForm($form, $form_state);
// Get S3FS Settings.
$s3_config = $this
->config('s3fs.settings');
if (!empty($s3_config)) {
if (!empty($cors_origin)) {
// Create an array of allowed CORS origins
$cors_origins = array_filter(explode(',', str_replace(' ', ',', $cors_origin)));
$allowed_origins = [];
foreach ($cors_origins as $origin) {
$allowed_origins[] = 'http://' . $origin;
$allowed_origins[] = 'https://' . $origin;
}
$this->s3Client
->putBucketCors([
// REQUIRED.
'Bucket' => $s3_config
->get('bucket'),
// REQUIRED.
'CORSConfiguration' => [
// REQUIRED.
'CORSRules' => [
[
'AllowedHeaders' => [
'*',
],
'ExposeHeaders' => [
'x-amz-version-id',
],
'AllowedMethods' => [
'POST',
],
'MaxAgeSeconds' => 3000,
'AllowedOrigins' => $allowed_origins,
],
[
'AllowedMethods' => [
'GET',
],
'AllowedOrigins' => [
'*',
],
],
],
],
]);
$this
->messenger()
->addMessage($this
->t("CORS settings have been succesfully updated at AWS CORS"));
}
else {
// If $form_state['values']['s3fs_cors_origin'] is empty, that means we
// need to delete their bucket's CORS config.
$this->s3Client
->deleteBucketCors([
'Bucket' => $s3_config
->get('bucket'),
]);
$this
->messenger()
->addMessage($this
->t("CORS settings have been deleted succesfully"));
}
}
else {
$this
->messenger()
->addMessage($this
->t('No values have been saved. Please check S3 Settings First'));
}
}