Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $form['comp_card_dl'] = [
- '#type' => 'button',
- '#value' => $this->t('Download'),
- '#ajax' => [
- 'callback' => '::generateCSV',
- 'progress' => [
- 'type' => 'throbber',
- ],
- ],
- '#name' => 'comp_card_download',
- ];
- public function generateCSV($form, FormStateInterface $form_state) {
- $trigger = $form_state->getTriggeringElement();
- if ($trigger['#name'] == 'gift_card_download') {
- $filename = $form['#gift_card_filename'];
- $data = $form['#gift_card_data'];
- }
- else if ($trigger['#name'] == 'comp_card_download') {
- $filename = $form['#comp_card_filename'];
- $data = $form['#comp_card_data'];
- }
- $path = Drupal::service('file_system')->realpath(file_default_scheme() . "://") . '/';
- $csv = fopen($path . $filename, 'w');
- foreach ($data as $id => $value) {
- array_unshift($value, $id);
- fputcsv($csv, $value);
- }
- fclose($csv);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement