3 namespace Drupal\file_module_test\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
9 * Form controller for file_module_test module.
11 class FileModuleTestForm extends FormBase {
16 public function getFormId() {
17 return 'file_module_test_form';
24 * An associative array containing the structure of the form.
25 * @param \Drupal\Core\Form\FormStateInterface $form_state
26 * The current state of the form.
28 * (optional) If the form should use #tree. Defaults to TRUE.
29 * @param bool $extended
30 * (optional) If the form should use #extended. Defaults to TRUE.
31 * @param bool $multiple
32 * (optional) If the form should use #multiple. Defaults to FALSE.
33 * @param array $default_fids
34 * (optional) Any default file IDs to use.
36 public function buildForm(array $form, FormStateInterface $form_state, $tree = TRUE, $extended = TRUE, $multiple = FALSE, $default_fids = NULL) {
37 $form['#tree'] = (bool) $tree;
39 $form['nested']['file'] = [
40 '#type' => 'managed_file',
41 '#title' => $this->t('Managed <em>@type</em>', ['@type' => 'file & butter']),
42 '#upload_location' => 'public://test',
43 '#progress_message' => $this->t('Please wait...'),
44 '#extended' => (bool) $extended,
46 '#multiple' => (bool) $multiple,
49 $default_fids = explode(',', $default_fids);
50 $form['nested']['file']['#default_value'] = $extended ? ['fids' => $default_fids] : $default_fids;
53 $form['textfield'] = [
54 '#type' => 'textfield',
55 '#title' => $this->t('Type a value and ensure it stays'),
60 '#value' => $this->t('Save'),
69 public function submitForm(array &$form, FormStateInterface $form_state) {
71 $uploads = $form_state->getValue(['nested', 'file']);
74 $uploads = $form_state->getValue('file');
77 if ($form['nested']['file']['#extended']) {
78 $uploads = $uploads['fids'];
82 foreach ($uploads as $fid) {
86 drupal_set_message($this->t('The file ids are %fids.', ['%fids' => implode(',', $fids)]));