HtaccessDownloadForm.php in Htaccess 8.2
File
src/Form/HtaccessDownloadForm.php
View source
<?php
namespace Drupal\htaccess\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Database\Database;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class HtaccessDownloadForm extends FormBase {
public function getFormID() {
return 'htaccess_admin_download';
}
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$path = $request
->getPathInfo();
$parts = explode('/', $path);
$id = $parts[7];
$htaccess_get = Database::getConnection()
->select('htaccess', 'h');
$htaccess_get
->fields('h');
$htaccess_get
->condition('id', $id);
$results = $htaccess_get
->execute();
$result = $results
->fetch();
$htaccess_content = $result->htaccess;
$htaccess_content = str_replace("", '', $htaccess_content);
$file_name = $result->name . '.htaccess';
$htaccess_folder = 'public://htaccess';
$htaccess_file = file_save_data($htaccess_content, "{$htaccess_folder}/{$file_name}", FILE_EXISTS_RENAME);
return new BinaryFileResponse($htaccess_file
->getFileUri(), 200, array(
'Content-Type' => 'application/octet-stream',
'Content-disposition' => 'attachment; filename=' . $file_name,
));
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}