Skip to content

Commit

Permalink
DAMO-284 | Form added.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-herczeg authored and l-besenyei committed Aug 26, 2024
1 parent da7c7f5 commit 99ff7e1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
8 changes: 8 additions & 0 deletions modules/damopen_upload/damopen_upload.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ damopen_upload.upload:
_title: 'Image upload'
requirements:
_permission: 'access content'
damopen_upload.image_form:
path: '/upload-image-form'
defaults:
_form: '\Drupal\damopen_upload\Form\DamopenUploadForm'
_title: 'Upload images'
requirements:
_permission: 'access content'
_access: 'TRUE'
66 changes: 66 additions & 0 deletions modules/damopen_upload/src/Form/DamopenUploadForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Drupal\damopen_upload\Form;

/**
* @file
* Contains \Drupal\damopen_upload\Form\DamopenUploadForm.
*/

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

/**
* Class DamopenUploadForm Provides a form for uploading files.
*
* @package Drupal\damopen_upload\Form
*/
class DamopenUploadForm extends FormBase {

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'student_registration_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['file'] = [
'#type' => 'dropzonejs',
'#title' => $this->t('File'),
'#description' => $this->t('Choose a file to upload.'),
];

$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Upload'),
];
return $form;
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Save the uploaded file.
$files = $form_state->getValue('file');
// Generate unique id.
$upload_id = \Drupal::service('uuid')->generate();
foreach ($files['uploaded_files'] as $file) {
$file = File::create([
'uri' => $file['path'],
'status' => 0,
]);
$file->set('field_upload_id', $upload_id);
dpm($upload_id);
$file->save();
}
// Redirect to the file listing page.
$form_state->setRedirect('damopen_upload.upload', ['upload_id' => $upload_id]);
}

}

0 comments on commit 99ff7e1

Please sign in to comment.