You are here

public function HtaccessDownloadForm::buildForm in Htaccess 8.2

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 FormInterface::buildForm

File

src/Form/HtaccessDownloadForm.php, line 31
Contains Drupal\htaccess\Form\HtaccessDownloadForm.

Class

HtaccessDownloadForm
Defines a form to configure RSVP List module settings

Namespace

Drupal\htaccess\Form

Code

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;

  // Remove utf8-BOM
  $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,
  ));
}